jQuery 复选框验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3932723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery checkbox validation
提问by ss888
I'm using the jQuery code below to validate a form. Now it all works great but I would like for the checkbox validation to use the .validator
rather than its current alert
, but when I add required="required"
to the checkbox input boxes, I get messages for all of the checkboxes, whereas I only need one.
我正在使用下面的 jQuery 代码来验证表单。现在一切都很好,但我希望复选框验证使用 the.validator
而不是它的 current alert
,但是当我添加required="required"
到复选框输入框时,我会收到所有复选框的消息,而我只需要一个。
$("#request-form").validator({
message: '<div><em/></div>',
position: 'top left',
offset: [3, 40]
});
$("#see").overlay({mask: '#999', fixed: false}).bind("onBeforeClose", function(e) {
$(".error").hide();
});
$('#request-form').submit(function(){
if(!$('#request-form input[type="checkbox"]').is(':checked')){
alert("Please check at least one.");
return false;
}
});
Hope this makes sense. Thanks in advance. S
希望这是有道理的。提前致谢。秒
回答by mcgrailm
Here is some code that will work with the JQuery Validation Plugin. I'm not sure if this will work with what you're using because I've never heard of it.
下面是一些可以与JQuery Validation Plugin一起使用的代码。我不确定这是否适用于您使用的产品,因为我从未听说过。
$("#request-form").validate({
rules: {
checkbox: {
required: 'input[type="checkbox"]:checked',
minlength: $('input[type="checkbox"]').length()
}
},
messages: {
checkbox: "Please check at least one.",
}
});
HTH
HTH