gpt4 book ai didi

javascript - 无法在表中添加动态行 - javascript

转载 作者:行者123 更新时间:2023-11-28 02:37:06 25 4
gpt4 key购买 nike

我正在尝试构建一种动态表。基本上,我希望用户输入一些字段并在“添加行”按钮上,用户输入的这些输入将填充下表等等,他可以输入他需要的行数。然后从表行中提取数据并调用服务上传。

我所做的是成功创建了添加行功能。但是有一个用户选择图像然后单击添加行按钮的输入,问题是图像被所有行上的前一个图像替换。

var inc = 1;

function add(tableID) {
console.log(inc);
var educationInstitute = $("#educationInstitute").val();
var educationQualification = $('#educationQualification').find(":selected").val();
var educationAdmDate = $("#educationAdmDate").val();
var educationGraDate = $("#educationGraDate").val();
//previewImage("educationImage", inc);
//var educationImage = $("#educationImage").val();

var fullPath = document.getElementById('educationImage').value;
if (fullPath) {
var startIndex = (fullPath.indexOf('\\') >= 0 ? fullPath.lastIndexOf('\\') : fullPath.lastIndexOf('/'));
var filename = fullPath.substring(startIndex);
if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
filename = filename.substring(1);
}

}
filename.replace(/ /g, '');
filename = filename.slice(0, -4);
alert(filename);
previewImage("educationImage", filename);

var markup = "<tr><td><input type='checkbox' name='record'></td><td>" + educationInstitute + "</td><td>" + educationQualification + "</td><td>" +
educationAdmDate + "</td><td>" + educationGraDate + "</td><td><img class='previewImage" + filename + "' src='' /></td></tr>";
$("#educationTable tbody").append(markup);
}

function del(row) {
$("table tbody").find('input[name="record"]').each(function() {
if ($(this).is(":checked")) {
$(this).parents("tr").remove();
}
});
}

function previewImage(id, filename) {
if (document.getElementById(id).files[0]) {
console.log("ID");
var reader = new FileReader();
reader.onload = function(e) {
//console.log(e.target.result);
$('.previewImage' + filename).attr('src', e.target.result);
inc += 1;
console.log(inc);
}
reader.readAsDataURL(document.getElementById(id).files[0]);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<fieldset style="min-height:100px;">
<legend>Education Details </legend>
<div class="row">
<button onclick="add('educationTable')">Add Table</button>
</div>
<div class="row">
<div class="col-md-2">
<div class="form-group">
<input type="text" class="form-control" name="educationInstitute" id="educationInstitute" placeholder="Institue Name" />
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<select class="form-control" name="educationQualification" id="educationQualification">
<option value="5">--- Qualification ---</option>
<option value="5">Option 1</option>
<option value="6">Option 2</option>
</select>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<input type="text" class="form-control datepicker" name="educationAdmDate" id="educationAdmDate" placeholder="Admission Date" />
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<input type="text" class="form-control datepicker" name="educationGraDate" id="educationGraDate" placeholder="Graduation Date" />
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<input type="file" class="form-control imageFileinput" name="educationImage" id="educationImage" data-show-preview="false" />
</div>
</div>

<table class="table table-responsive table-bordered order-list" id="educationTable">
<thead>
<tr>
<th>Check</th>
<th>Institute Name</th>
<th>Qualification</th>
<th>Admission Date</th>
<th>Graduation Date</th>
<th>Degree Scanned Image</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<div class="row">
<i class="fa fa-plus-circle fa-2x fa-fw" style="padding-left: 15px;" onclick="AddTableRow.del('educationTable')"></i>
</div>
</fieldset>

谁能告诉我,我做错了什么?我已经花了 2 天时间研究它。

最佳答案

另一种选择是在 add 上制作输入的副本并将其附加到 td 中的行此字段也可以隐藏,当你想获取可以使用 jquery 获取所有相应输入的文件。

  var markup = `
<tr>
<td><input type='checkbox' name='record'></td>
<td> ${educationInstitute}</td>
<td> ${educationQualification}</td>
<td> ${educationAdmDate}</td>
<td> ${educationGraDate}</td>
<td class='file_holder'>
<img class='previewImage${filename}' src='' />
// append the corresponding input file here for later use
</td>
</tr>`;
$("#educationTable tbody").append(markup);
$("#educationImage").clone().appendTo("#educationTable tbody tr:last-child .file_holder");

这是例子 jsfiddle

关于javascript - 无法在表中添加动态行 - javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46548958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com