如何获取< input type =“文件">中所有文件的名称.具有多属性的输入? [英] How to get names of all files in the <input type="file"> input that has the multiple attribute?

查看:53
本文介绍了如何获取< input type =“文件">中所有文件的名称.具有多属性的输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上的输入具有 multiple 属性:

I have a input with the multiple attribute on my page:

<input type="file" accept="video/*" required multiple id="input-file-video">

如果用户一次选择了3个文件,则输入将仅显示:"3个文件已选择",但是我想获取所有已选择文件的名称.我还尝试访问输入的 value 属性:

If the user selects, let's say, 3 files at once, the input is going to display just : "3 files selected", but I want to get the names of all files that are selected. I also tried accessing the value property of the input:

document.getElementById("input-file-video").value;

但这只返回第一个文件的名称.

But that returns just the name of the first file.

有什么办法吗?

推荐答案

尝试一下,

var inp = document.getElementById('input-file-video');
for (var i = 0; i < inp.files.length; ++i) {
  var name = inp.files.item(i).name;
  alert("file name: " + name);
}

在jQuery中, $('#input-file-video').get(0).files; 将返回已上传文件的列表.因此,您可以遍历并获取文件名.

In jQuery, $('#input-file-video').get(0).files; will return the list of files that are uploaded. So you can loop through and get the files names.

这篇关于如何获取&lt; input type =“文件"&gt;中所有文件的名称.具有多属性的输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆