2012-01-11 92 views
4

要使用jQuery獲取<input type="file" name="upload">的值,可以使用$('input').val()如何通過jQuery獲取多文件輸入的更改值?

但是,使用相同的方法獲取<input type="file" name="upload[]" multiple>(多文件輸入)的值只返回一個文件,而不是全部文件。

有沒有不同的方法來做到這一點?

+0

您是否找到了解決方案?答案根本沒有幫助。 – Alqin 2013-03-18 22:57:33

+3

@Alqin是的。檢查出http://stackoverflow.com/questions/14035530/how-to-get-value-of-html-5-multiple-file-upload-variable-using-jquery – 2013-03-18 23:11:38

+0

,但該鏈接鏈接到純js解決方案,而不是jquery – dsdsdsdsd 2013-10-08 07:48:06

回答

2

試控制檯到

document.forms[index].elements[index].files 

緩存值通過他們

-1

的循環這是因爲,當您使用$('input').val()它只返回第一個這些投入的價值。您需要循環以獲取所有這些值。請參見下面的代碼:

var $uploads = $('input'); 

$uploads.each(function() { 

    console.log($(this).val()); 

}); 
1

我使用了一些代碼從另一個帖子...

$('input[type=file]').val() 

..和添加的每個特定的文件輸入字段的ID。下面的代碼從輸入中獲取路徑,只從中提取文件名,並在兩個不同文件上傳字段的不同字段中顯示該值。如果你有一大堆,你可以創建一個基於此代碼的循環:

$('#File1').change(function(){ // when you leave the File1 form field 
    var filename1 = $('input[type=file][id=File1]').val().split('\\').pop(); // get just the filename 
    $('#DL_Filename1').val(filename1); // and place it in the DL_Filename1 box 
}); // end change 

$('#File2').change(function(){ // when you leave the File2 form field 
    var filename2 = $('input[type=file][id=File2]').val().split('\\').pop(); // get just the filename 
    $('#DL_Filename2').val(filename2); // and place it in the DL_Filename2 box 
}); // end change 
+1

也許你可以提供「來自另一個帖子的代碼...」的歸屬...謝謝。 – Kev 2012-09-22 22:46:19

相關問題