获取输入类型=“文件”当它选择多个文件时的值 [英] Get input type="file" value when it has multiple files selected
问题描述
$ b多文件上传控件
$ b
从HTML5输入type =file允许用户通过添加multiple =multiple来选择多个文件:
< input type =filemultiple =multiple/>
我的问题是:如何获得输入值?使用.value时,它只返回所选第一个文件的文件名,但选择多个文件时,我无法查看其他文件。
我有什么:
< input type =filemultiple =multipleonchange =alert(this.value)
onmouseout =alert(this.value)/>
正如我告诉过你的,是只显示其中一个选定文件的名称。
注意:我不想编辑该值(我知道这是不可能的)文件名
感谢!
所选文件存储在一个数组中: [input] .files
例如,您可以访问项目
//假设有一个文件输入ID为my-input` ...
var files = (var i = 0; i< files.length; i ++)
{
alert(files [ i] .name);
}
对于jQuery舒适的人来说,它也很容易
//假设有一个文件输入ID为'my-input` ...
var files = $(#my-input)[0 ] .files;
for(var i = 0; i< files.length; i ++)
{
alert(files [i] .name);
}
Possible Duplicate:
Retrieving file names out of a multi-file upload control with javascript
From HTML5 input type="file" allows users to choose multiple files by adding the multiple="multiple" :
<input type="file" multiple="multiple" />
My question is: how can I get the value of that input? When using the .value it only returns the filename of the first file selected, but when choosing more than one I am not able to view the other ones.
What I have:
<input type="file" multiple="multiple" onchange="alert(this.value)"
onmouseout="alert(this.value) />
which as I told you, is only showing the name of one of the selected files.
NOTE: I don't want to edit the value (I know that is not possible) only the name of the files
Thanks!
The files selected are stored in an array: [input].files
For example, you can access the items
// assuming there is a file input with the ID `my-input`...
var files = document.getElementById("my-input").files;
for (var i = 0; i < files.length; i++)
{
alert(files[i].name);
}
For jQuery-comfortable people, it's similarly easy
// assuming there is a file input with the ID `my-input`...
var files = $("#my-input")[0].files;
for (var i = 0; i < files.length; i++)
{
alert(files[i].name);
}
这篇关于获取输入类型=“文件”当它选择多个文件时的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!