在用户单击我的表单按钮后,我尝试检查我的输入字段中的值,但它没有进入输入循环来检查必填字段。这不适用于点击事件吗?这就是我所说的
$('#submit_button').click(function(){
var formValid = true;
$(".required input").each(function(){
if ($.trim($(this).val()).length == 0){
$(this).addClass("error_msg");
formValid = false;
}
else{
$(this).removeClass("error_msg");
}
});
// post form if no errors are seen
});
我的输入 HTML 如下所示:
<input class="required" type="text" value="" name="test" id='test_id' size="80"/>
我错过了什么?这看起来应该可行...
最佳答案
尝试
$("input.required")
而不是
$(".required input")
您可以使用 jQuery 提交表单 submit功能:
$('#submit_button').click(function(){
var formValid = true;
$("input.required").each(function(){
if ($.trim($(this).val()).length == 0){
$(this).addClass("error_msg");
formValid = false;
}
else{
$(this).removeClass("error_msg");
}
});
if(formValid) $('#yourForm').submit();
else return false; //if the button is a submit
});
关于jquery - 无法通过单击事件检查必填字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7375055/