繁体   English   中英

Fileupload在Jquery中最近的tr

[英]Fileupload Closest tr in Jquery

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同名的局部变量

你做得对,只需更换

var file = fup.file[i];//I am getting exception here

var file = fup[i].files[0];// no Exception here.

这将工作正常。

我认为问题在于您选择的输入。 您可以使用以下选项:

$(":file")

要么

$("table tr :input[type='file']").each(function (i,v) {
      var file=$(v);
      //Your code goes here
});

工作示例JsFiddle

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM