我已经收集了所需表单元素的数组(我认为),并添加了“模糊”侦听器。
var formInputs = $(':input').filter('[required]');
formInputs.each(function(i) {
$(this).on('blur', function() { // Each time we leave a 'required' field, check to see if we can activate the 'submit' button.
submitEnabler(formInputs);
});
});
因此,一旦有人离开了这些字段之一,我想使用 .every() 遍历此数组并检查这些字段是否有效 - 即它们是否具有我定义的“成功”类。
function isValid(input) {
return input.hasClass('is_glowing_success');
}
function submitEnabler(inputs) {
console.log(inputs.every(isValid));
}
我不断回来:
Uncaught TypeError: inputs.every is not a function
at submitEnabler
现在,我可以做这样的事情……
for (var i = 0; i < inputs.length; i++) {
if ($(inputs[i]).hasClass('is_glowing_success')) {
console.log('yes');
} else {
console.log('no');
}
}
但是,为什么我不能只使用: Array.Prototype.every() ?
原文由 CodeFinity 发布,翻译遵循 CC BY-SA 4.0 许可协议
jQuery 没有
.every()
方法。.every
定义在Array.prototype
。You can use
.toArray()
to convert jQuery object to anArray
, within.every()
callback function pass currentDOM
element tojQuery()
获取元素的 jQuery 对象表示,其中.hasClass()
可以链接。