我有以下 HTML:
<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');
});
我想要做的是,仅当所有 5 个 input:file
元素通过验证(仅限 pdf 扩展名)时才切换 #label
。或者没有选择文件。
目前,如果我首先选择 .jpg
,则会显示 #label
,然后如果我选择 .pdf
,则会显示 #label
消失。这是不正确的,因为在五个 input:file
元素之一中仍然选择了 .jpg
。
编辑: fiddle here
最佳答案
这个怎么样?
$('input:file').change(function(){
$('input:file').each(function(){
var ext = $(this).split('.').pop().toLowerCase();
$('#label').toggle(ext != 'jpg');
});
});
关于jQuery 验证多个输入 :file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12748106/