javascript 如何计算选定的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9232633/
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
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:00:30 来源:igfitidea点击:
How to count selected files
提问by Linas
I have very simple form, with input like this:
我有非常简单的表格,输入如下:
<input id="my_id" multiple="true" type="file" name="image_name[]" />
Now i have two questions:
现在我有两个问题:
- how can i count selected files with jQuery or pure JavaScript?
- how can i limit file selection to, let's say 10, because now it's infinite?
- 如何使用 jQuery 或纯 JavaScript 计算选定的文件?
- 我怎样才能将文件选择限制为 10,因为现在它是无限的?
回答by dku.rajkumar
in case of input type file
the value is stored in array
as files
with key name
.
在的情况下input type file
的值被存储在array
作为files
与关键name
。
$('input#my_id').change(function(){
var files = $(this)[0].files;
if(files.length > 10){
alert("you can select max 10 files.");
}else{
alert("correct, you have selected less than 10 files");
}
});
fiddle example : http://jsfiddle.net/nze2B/3/
小提琴示例:http: //jsfiddle.net/nze2B/3/
回答by Lloyd
pure javascript:
纯javascript:
document.getElementById("my_id").addEventListener("change", function() {
console.log(this.files.length);
});