HTML5:如何计算来自多输入字段的文件的长度 [英] HTML5: How to count the length of the files from the multiple-input field

查看:193
本文介绍了HTML5:如何计算来自多输入字段的文件的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

  alert($($($($($($ '#myForm input [type = file]')。files.length); 
< input type =filemultiple =multiplename =file []/>

错误:


$(#myForm input [type = file])。files are undefined

我不想在我的输入字段中使用ID。

  alert(document.getElementById('file')。files。长度); 

< input type =filemultiple =multiplename =file []id =file/>


解决方案

尝试使用 .get 方法:



< pre $ $('#myForm input [type = file]')。get(0).files.length

请注意,如果您有多个与您的选择器匹配的DOM元素,则会返回第一个DOM元素,如果没有,则显然会抛出异常。

How can I use jquery to count the length of the files from the multiple-input field?

alert($('#myForm input[type=file]').files.length);
<input type="file" multiple="multiple" name="file[]"/>

error:

$("#myForm input[type=file]").files is undefined

It works find with plain js but I don't want to use ID in my input field.

alert(document.getElementById('file').files.length);

<input type="file" multiple="multiple" name="file[]" id="file"/>

解决方案

Try getting the native DOM element using the .get method:

$('#myForm input[type=file]').get(0).files.length

Note however that if you have multiple DOM elements matching your selector this will return the first one and if you have none it will obviously throw an exception.

这篇关于HTML5:如何计算来自多输入字段的文件的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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