3

我需要使用 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');
})

有人知道我要去哪里错了吗?所有帮助表示赞赏

4

1 回答 1

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/

于 2012-09-24T00:32:47.583 回答