2012-01-11 85 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

相关问题