0
$("input[type=text]").on('focusout', function(){
    if($(this).each().hasClass('valid')){
        alert('go');
    }
});

当用户完成填写所有字段时,我想检查该字段是否通过了我的验证,检查他们每个人是否都具有“有效”类..但是在控制台中我收到了这个错误

Uncaught TypeError: Cannot call method 'call' of undefined 
4

1 回答 1

3

.each方法用于遍历 jQuery 集合,您对它的使用是错误的,如果您想检查所有输入是否都有类,valid您可以将 Input 集合的长度与过滤后的长度进行比较.valid。或使用.not()方法:

var $inputs = $("input[type=text]");

$inputs.on('blur', function(){
    if ( $inputs.length === $inputs.filter('.valid').length ) {
       // all fields are valid
    }
});

使用.not()方法:

if ( !$inputs.not('.valid').length ) {
   // all fields are valid
} else {
   // at least one of them is not valid
}
于 2013-09-12T10:07:03.050 回答