【发布时间】:2013-12-05 19:37:57
【问题描述】:
我的 MVC4 应用程序中有 3 个字段(fieldA、fieldB、fieldC),我根据 fieldA 的值显示/隐藏 fieldB 和 fieldC。尽管显示/隐藏这些字段没有问题,但我无法阻止 fieldB 和 fieldC 在隐藏或禁用时被验证。我尝试了 3-4 种不同的方法,即在 showHideFields() 方法中使用禁用的隐藏属性,在 jQuery("#...").validate 方法中使用“ignore”属性。但是它们都没有正常工作。您能否提供这样一种解决方案来做到这一点?
剃须刀:
function showHideFields() {
var $index = $('#fieldA').val();
if ($index == '1') {
$('#fieldB').show();
$('#fieldC').show();
}
else {
$('#fieldB').hide();
$('#fieldC').hide();
}
}
jQuery(function () {
jQuery("#fieldA").validate({
expression: "if (VAL) {return true;} else {return false;}",
message: "Please fill in this field"
});
jQuery("#fieldB").validate({
expression: "if (VAL) {return true;} else {return false;}",
message: "Please fill in this field"
});
jQuery("#fieldC").validate({
expression: "if (VAL) {return true;} else {return false;}",
message: "Please fill in this field"
});
...
}
【问题讨论】:
-
默认忽略隐藏字段。您使用的是什么版本的插件?
-
您对
.validate()的调用中的代码毫无意义...您确定您使用的是the jQuery Validate plugin? -
请阅读上面的my previous comment,我要求您确认您正在使用哪个插件。此外,在标记问题时要更加小心。 jquery-validate 标签不适用于 the plugin you said you're using 在我的回答中的 cmets。
标签: javascript jquery asp.net-mvc validation