Javascript 使用jQuery计算输入框中的文件数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14448635/
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
Count number of files in input box with jQuery?
提问by Dylan Cross
How can I count the number of files that are in the file upload input using jQuery?
如何使用 jQuery 计算文件上传输入中的文件数量?
I have tried this but it always returns 1:
我试过这个,但它总是返回 1:
$("body").on("change", ".create-album .custom-file-input .createAlbumFileUpload", function(){
var numFiles = $("input:file", this).length;
alert(numFiles);
});
My HTML:
我的 HTML:
<form enctype="multipart/form-data" class="createAlbumFileUpload">
<input type="file" name="uploadFile[]" multiple="multiple"/>
</form>
I found this jsFiddleon another question asking the same thing, however I can't see why mine isn't working?
我在另一个问题上发现了这个jsFiddle,但我不明白为什么我的不工作?
回答by Kevin B
The fiddle you provided contains the answer.
您提供的小提琴包含答案。
var numFiles = $("input:file", this)[0].files.length;
回答by Smit Patel
You can also try with this,
你也可以试试这个
Take the input in Div and give an id,
获取 Div 中的输入并给出一个 id,
var count = $('#divPhoto').children().length; // You'll get the length in this.
回答by Rajpal Singh
You can easily check with below code and https://jsfiddle.net/vvz3a4qp/2/:
您可以使用以下代码和https://jsfiddle.net/vvz3a4qp/2/轻松检查:
HTML:
HTML:
<form name="myForm" enctype="multipart/form-data">
<input type="text" name="name">
<input type="file" name="name1" >
<input type="file" name="name2" ><br><br>
<span id="total"></span>
</form>
JS:
JS:
$("body").change(function(){
alert($(":file").length);
$("#total").html($("input[type=file]").length);
});

