如何使用jquery检查所有值是否为空。
<input type='text' class='req-in' name='fname'>
<input type='text' class='req-in' name='lname'>
<input type='text' class='req-in' name='nam2'>
<input type='text' class='req-in' name='nam4'>
我试过这个,但它只适用于第一个input
if($('.req-in')).val() == ''{
// code here
$(this).after('<p>empty!</p>');
}
答案 0 :(得分:3)
您需要迭代元素并检查值:
$('.req-in').each(function () {
if (!this.value) {
$(this).after('<p>empty!</p>');
}
});
您也可以使用.filter()
method:
$('input.req-in').filter(function () {
return !this.value;
}).after('<p>empty!</p>');
答案 1 :(得分:2)
您应该使用以下代码.each():
$('.req-in').each(function(){
if( $(this).val()==""){
$(this).after('<p>empty!</p>');
}
});