检查所有值是否为空或使用jquery填充

时间:2015-04-09 02:23:45

标签: javascript jquery html

如何使用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>');
}

2 个答案:

答案 0 :(得分:3)

您需要迭代元素并检查值:

Example Here

$('.req-in').each(function () {
    if (!this.value) {
        $(this).after('<p>empty!</p>');
    }
});

您也可以使用.filter() method

Updated Example

$('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>'); 
 }
});
相关问题