HTML5获取输入文件计数

时间:2012-09-24 00:28:18

标签: jquery html5

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

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');
})

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

1 个答案:

答案 0 :(得分:6)

files不是jQuery对象的属性。

改变这个:

$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/