HTML5获取输入文件计数 [英] HTML5 Get input file count on change

查看:168
本文介绍了HTML5获取输入文件计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用html5 File API来获取输入文件元素中选择的文件数量,并在另一个元素中设置值。

I need to use the html5 File API to get the quantity of files selected in the input file element and set the value in another element.

HTML

<div class="form-row">
    <label>Main Image</label>
    <div class="form-text">None Selected</div> 
    <input type="file" class="txtbox" name="proImage" id="proImage" value="" />
</div>

Jquery

$('input[type=file]').change(function () {
    $fileCount = $(this).files.length;
    $(this).parent('.form-row').find('.form-text').html($fileCount + 'Selected');
})

任何人都知道我哪里出错了?所有帮助赞赏

Anyone know where I'm going wrong? All help appreciated

推荐答案

files 不是jQuery对象的属性。

files is not a property of jQuery object.

更改此:

$fileCount = $(this).files.length;

收件人:

$fileCount = this.files.length;

您还可以缩小选择器:

$('input[type=file]').change(function () {
    fileCount = this.files.length;
    $(this).prev().text(fileCount + 'Selected');
})

http://jsfiddle.net/hHgEF/

这篇关于HTML5获取输入文件计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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