以下是我所拥有的:
$("#myform").validate({
ignore: "",
rules: {
MaritalStatusDate: {
required: {
depends: function () {
return $("input[type='radio'].maritalStatusRadio:checked").val() != '1';
}
}
}
},
messages: {
MaritalStatusDate: {
required: "You must enter a marital status date"
}
}
});
MaritalStatusDate是我隐藏字段的名称/ ID。我尝试了大约100次不同的代码迭代,每次都会提交表单。
任何想法?
编辑:添加html。
<script src="/Scripts/jquery-2.0.3.min.js"></script>
<script src="/Scripts/jquery.validate.1.11.1.min.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js"></script>
<form action="/cont/meth" id="myform" method="post">
<input class="maritalStatusRadio" data-val="true" data-val-required="The MaritalStatusAnswer field is required." id="maritalStatus_1" name="MaritalStatusAnswer" type="radio" value="1" />
<input class="maritalStatusRadio" id="maritalStatus_2" name="MaritalStatusAnswer" type="radio" value="2" />
<input class="maritalStatusRadio" id="maritalStatus_3" name="MaritalStatusAnswer" type="radio" value="3" />
<input class="maritalStatusRadio" id="maritalStatus_4" name="MaritalStatusAnswer" type="radio" value="4" />
<input data-val="true" data-val-date="The field MaritalStatusDate must be a date." id="MaritalStatusDate" name="MaritalStatusDate" type="hidden" value="" />
<input type="submit" value="go" />
</form>
答案 0 :(得分:1)
如果没有显示您的HTML标记,我只是猜测它的外观......
<form id="myform">
<input type="hidden" name="MaritalStatusDate" />
<input type="radio" name="radio" value="1" class="maritalStatusRadio" />
<input type="radio" name="radio" value="0" class="maritalStatusRadio" />
<input type="submit" />
</form>
在这种情况下,这有效......
depends: function(element) {
return !($("input[type='radio'].maritalStatusRadio[value='1']").is(":checked"));
}
DEMO:http://jsfiddle.net/nVc2N/
旁注:如果你想忽略任何事情,那真的应该是ignore: []
。