我正在尝试使用自定义验证功能来检查是否至少选择了一个复选框。但是验证功能没有触发。我错过了什么?
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
<script type="text/javascript" src="../Scripts/jquery-1.8.0.js"></script>
<script type="text/javascript" src="../Scripts/jquery.validate.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$.validator.addMethod("fruits", function(value, elem, params) {
if($(".fruits:checkbox:checked").length > 0){
return true;
}else {
return false;
}
},"pick at least one plz");
$("#form1").validate({
rules:{
fruits:{
fruits: true
}
},
submitHandler: function(form) {
alert("submitting...");
form.submit();
},
invalidHandler: function(form){
alert("invalid form");
}
});
});
</script>
</head>
<body>
<form id="form1">
<input type="checkbox" class="{fruits: true}" name="fruits" id="chkApple" value="1" />Apple<br />
<input type="checkbox" class="fruits: true}" name="fruits" id="chkBanana" value="2" />Banana<br />
<input type="submit" value="Submit" />
</form>
</body>
</html>