使用 jQuery 获取多个文件选择的所有值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13652955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get all values of multiple file select with jQuery
提问by Bob van Luijt
Possible Duplicate:
Retrieving file names out of a multi-file upload control with javascript
Got this:
明白啦:
<input type="file" name="file1" multiple="multiple" />
Using this in jQuery:
在 jQuery 中使用它:
$("input[name=file1]").change(function() {
$("input[name=file]").val($("input[name=file1]").val());
});
Now my problem is this: It only gives the first selected file, I would like to have them all... How to do this? Thanks!
现在我的问题是这样的:它只给出第一个选定的文件,我想把它们全部...怎么做?谢谢!
回答by j08691
Although I linked to a possible dupe in the comments, it's a pure JavaScript solution. Here's a jQuery version if you like: jsFiddle example
尽管我在评论中链接到了一个可能的骗局,但它是一个纯 JavaScript 解决方案。如果您喜欢,这里有一个 jQuery 版本:jsFiddle 示例
$("input[name=file1]").change(function() {
var names = [];
for (var i = 0; i < $(this).get(0).files.length; ++i) {
names.push($(this).get(0).files[i].name);
}
$("input[name=file]").val(names);
});?
回答by TNCodeMonkey
You are looking for the 'files' property, which returns a filelist:
您正在寻找 'files' 属性,它返回一个文件列表:
var ele = document.getElementById($(this).attr('id'));
var result = ele.files;
回答by Alexander North
Use .length()
to get the number of files then use a while statement to do the same for all files, increasing the count by 1 each time :)
使用.length()
获得的文件数量,然后使用while语句来完成对所有文件是相同的,按1增加计数:)