有一个简单的表格。我需要验证至少有一个联系人有本地单选按钮选中是。我如何使用 JQ Validate 做到这一点?
这是小提琴
http://jsfiddle.net/davetoolin/dGsQR/1/
这是html
<form id="contact" method="post" action="#">
<fieldset>Contact 1
<input class="name" id="name1" type="text" name="name1" size="20" maxlength=30>
<br>Local:
<input class="local" id="local1" type="radio" name="local1" Value="Y">Yes
<input class="local" id="local1" type="radio" name="local1" Value="N">No
<p>Contact 2
<input class="name" id="name2" type="text" name="name2" size="20" maxlength=30>
<br>Local
<input class="local" id="local2" type="radio" name="local2 " Value="Y ">Yes
<input class="local" id="local2" type="radio" name="local2 " Value="N ">No
<p>Contact 3
<input class="name" id="name3" type="text" name="name3" size="20" maxlength=30>
<br>Local
<input class="local" id="local3" type="radio" name="local3 " Value="Y ">Yes
<input class="local" id="local3" type="radio" name="local3 " Value="N ">No
<P>
<input type="submit" />
</fieldset>
和 JS
$('#submit').click(function() {
$('#mailer').submit();
return false;
});
$(document).ready(function () {
$("#contact").validate({
rules: {
name1: {
required: true
},
local1: {
required: function(element) {
return $('.local:checked').val() == "Y";
}
messages: {
name1: {
required: "Name Required"
},
local1: {
required: "At least one contact must be local"
}
}
});
});