我有一个表单,其中有几个输入字段,类为“inputfields”,如果为空,则需要验证每个字段,警报“字段名称为空”,否则返回 true,我的 jquery 代码无法正常工作,在控制台日志中不断收到错误,有人可以帮忙吗? ?
jQuery(document).ready(function() {
$('.inputfields').each.on('change keyup' ,function(){
var ope = $(this).attr('name');
if (ope.val()==''){
alert(ope+'is empty');
}else {
console.log(ope);
}
});
});
最佳答案
我相信您想要检查不同类型的输入字段,因此想要使用 change
和 keyup
。
我根据您的代码尝试了一些方法,但是只有当您想验证文本类型输入字段时,以下解决方案才有效。对于 select
或其他输入类型,您必须在循环内进行更多检查,或者必须采用其他方式进行验证。
$('.inputfields').each(function() {
$(this).bind('change keyup', function() {
var ope = $(this).attr('name');
if ($(this).val() == '') {
console.log(ope+'is empty');
} else {
console.log(ope+ ' : ' + $(this).val());
}
});
});
希望这会引导您找到您想要的解决方案。
关于javascript - jQuery 表单字段验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26731920/