我有以下HTML:jQuery的验证多个输入:文件
<input id="upload1" type="file" /><br/>
<input id="upload2" type="file" /><br/>
<input id="upload3" type="file" /><br/>
<input id="upload4" type="file" /><br/>
<input id="upload5" type="file" /><br/>
<span id="label" style="color:red; display:none">
Wrong filetype!
</span>
而下面的jQuery:
$('input:file').change(function(){
var ext = $(this).val().split('.').pop().toLowerCase();
$('#label').toggle(ext != 'pdf');
});
我想要做什么,只是切换的的#label
如果所有5 input:file
元素通过验证(仅限pdf扩展名)。或者没有选择文件。
目前,如果我先选择.jpg
,则显示#label
,那么如果我选择.pdf
,则#label
消失。这是不正确的,因为在五个input:file
元素之一中仍然有一个.jpg
被选中。
编辑:小提琴here
管理这个答案的变化来做到这一点。不过@Zathrus Writer是正确的,需要使用一个持久变量。标记这是因为这个答案。 –