簡體   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