HTML
<table>
<tr>
<td> <input type="file" /> </td>
<td> <input type="button" id="btn" /> </td>
</tr>
<tr>
<td> <input type="file" /> </td>
<td> <input type="button" id="btn" /> </td>
</tr>
<tr>
<td> <input type="file" /> </td>
<td> <input type="button" id="btn" /> </td>
</tr>
</table>
JQUERY
var formData = new FormData();
var tr = $(this).closest('tr');
var fup = $(tr).find("input[type='file']");
var totalFiles = fup.length;//Here, getting total file count
for (var i = 0; i < totalFiles; i++) {
var file = fup.file[i];//I am getting exception here
formData.append("fupUpdate", file);
}
我无法获取附加到 formdata 的文件列表。
Error Message: "Cannot read property '0' of undefined"
请协助我解决问题。
最佳答案
试试这段代码
var fData = new FormData();
var tr = $('table').closest('tr');
var fup = $('tr').find("input[type='file']");
var totalFiles = fup.length;//Here, getting total file count
for (var i = 0; i < totalFiles; i++) {
var file = fup[i].files[0];//Exception will not occur here
console.log(file)
fData.append("fupUpdate", file);
}
不要创建与 FormData 同名的局部变量
关于javascript - Jquery 中的文件上传最近的 tr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38525663/