我正在使用jquery验证插件进行表单字段验证。我在表单中有多个复选框。我尝试了几种不同的逻辑方法'至少需要选择一个',但没有任何工作,但当我尝试使用验证插件时,它可以正常使用此代码。如果你能告诉我如何在验证插件中加入以下代码
<script type="text/javascript">
$(document).ready(function () {
$('.submit').click(function() {
checked = $("input[type=checkbox]:checked").length;
if(!checked) {
// <form:errors path="chk" cssClass="errors" element="div" />
$(".form-error").text("* You must check at least one checkbox.").show();
// alert("You must check at least one checkbox.");
return false;
}
});
});
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script>
$(document).ready(function(){
$("#myForm").validate({
rules: {
crcode: "required",
dtype: "required",
pview: "required",
prview: "required",
},
messages: {
crcode: "Rate code is required",
dtype: "Dwell type is required",
pview: "Public view is required",
prview: "Private view is required",
}
});
});
</script>
Here are my checkboxes
<div class="[ form-group ]" style="font-family: KlavikaWebBasicBold;" >
<input type="checkbox" name="chkbox" id="fancy-checkbox-default1" autocomplete="off" class="fancy" />
<div class="[ btn-group ]">
<label for="fancy-checkbox-default1" class="[ btn btn-default ]">
<span class="[ glyphicon glyphicon-ok ]"></span>
<span> </span>
</label>
<label for="fancy-checkbox-default1" class="[ btn btn-default active ]">
Video+Internet
</label>
</div>
</div>
<div class="[ form-group ]" style="font-family: KlavikaWebBasicBold;">
<input type="checkbox" name="chkbox" id="fancy-checkbox-default2" autocomplete="off" class="fancy" />
<div class="[ btn-group ]">
<label for="fancy-checkbox-default2" class="[ btn btn-default ]">
<span class="[ glyphicon glyphicon-ok ]"></span>
<span> </span>
</label>
<label for="fancy-checkbox-default2" class="[ btn btn-default active ]">
Video+Phone
</label>
</div>
</div>
<div class="[ form-group ]" style="font-family: KlavikaWebBasicBold;">
<input type="checkbox" name="chkbox" id="fancy-checkbox-default3" autocomplete="off" class="fancy" />
<div class="[ btn-group ]">
<label for="fancy-checkbox-default3" class="[ btn btn-default ]">
<span class="[ glyphicon glyphicon-ok ]"></span>
<span> </span>
</label>
<label for="fancy-checkbox-default3" class="[ btn btn-default active ]">
Video+Security
</label>
</div>
</div>
答案 0 :(得分:2)
如果你能告诉我如何在验证插件中加入以下代码
$('.submit').click(function() {
checked = $("input[type=checkbox]:checked").length;
if (!checked) { ....
你没有。
该代码与jQuery Validate插件无关,可以完全删除。使用jQuery Validate插件时,您不需要为复选框验证编写完全独立的函数, NOR您是否需要任何类型的点击处理程序。
您只需要在复选框名称上声明required
规则,插件就会自动生成至少一个复选框。
$(document).ready(function(){
$("#myForm").validate({
rules: {
chkbox: "required", // <- add this
crcode: "required",
dtype: "required",
pview: "required",
prview: "required",
},
messages: {
chkbox: "* You must check at least one checkbox.", // <- add this
crcode: "Rate code is required",
dtype: "Dwell type is required",
pview: "Public view is required",
prview: "Private view is required",
}
});
});
DEMO:http://jsfiddle.net/42t2twLo/
使用the errorPlacement
option可以方便地放置此复选框错误消息。
errorPlacement: function(error, element) {
if (element.is(":checkbox")) {
error.insertBefore(element); // custom placement example
} else {
error.insertAfter(element); // default placement
}
}