2017-01-11 39 views
-2

所有ID元素我有形式:jQuery的獲取與文件類型

<form id="formUpload" enctype="multipart/form-data"> 
    <input type="text" name="name" id="name"> 
    <input type="file" id="TextFile" name="TextFile"> 
    <input type="file" id="ImageFile" name="ImageFile"> 
    <input type="file" id="f2" name="f2"> 
    <input type="submit" id="sub"> 
</form> 

我想我的所有文件apend到FORMDATA:

$('#input:file').each(function() { 
    fd.append(this.id, document.getElementById(this.id).files[0]); 
}); 

但是,這是行不通的。我如何從元素類型文件中獲取所有ID?

+0

你的選擇是說給我的元素與'ID =「輸入」',是一個文件 – epascarello

+0

'輸入:file'就足夠了 – haim770

回答

0
$(document).ready(function(){ 
    $("[type='file']").each(function(){ 
    alert($(this).attr("id")); 
    }); 
}); 

這將返回與輸入類型= 「文件」 的所有ID。

提琴手:https://jsfiddle.net/o68uhb8p/

+0

謝謝,這對我的作品 –

0

你可以試試這個:

var file = 
 
$('input[type=file]').map(function(){ 
 
    return this.id; 
 
}).get(); 
 
console.log(file);// this will print all the ids of type file
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="file" id="TextFile" name="TextFile"> 
 
<input type="file" id="ImageFile" name="ImageFile"> 
 
<input type="file" id="f2" name="f2">