我的mvc网络申请表上有一个邮政编码查找器是可选的。如果我没有得到邮政编码匹配,我用我自己的自定义错误消息添加了.validation-summary-errors。纠正错误后,删除.validation-summary-errors中的所有类邮政编码实例并应用隐藏方法。问题是隐藏validation-summary-errors div会在提交表单时禁用验证弹出窗口以显示其他表单错误。
我的javascript代码
$("#AddressFinder").click(function () {
var postcode = $("#Address_Postcode").val();
$(".postcode-error").remove();
$(".validation-summary-errors").hide();
if (!postcode) {
$(".validation-summary-errors ul").append("<li class='postcode-error'>Please include a postcode to search.</li>");
$(".validation-summary-errors").show();
}
else {
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json",
url: "/Postcode/GetAddress",
data: "{ 'postcode':'" + postcode + "' }",
success: function (data) {
if ($.isEmptyObject(data)) {
$(".validation-summary-errors").append("<li class='postcode-error'>There are no addresses matching your postcode</li>");
$(".validation-summary-errors").show();
}
else {
vm.addresses(data);
addressList.show();
}
}
});
}
});
如何在不将表单提交给控制器时禁用valiidation-summary-errors div?
答案 0 :(得分:0)
管理以解决我的问题。你不应该使用hide()或show()。只需添加或删除validation-summary-validation。
$("#AddressFinder").click(function () {
var postcode = $("#Address_Postcode").val();
if (!postcode) {
$(".validation-summary-errors ul").append("<li class='postcode-error'>Please include a postcode to search.</li>");
$(".validation-summary-errors").removeClass("validation-summary-valid");
}
else {
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json",
url: "/Postcode/GetAddress",
data: "{ 'postcode':'" + postcode + "' }",
success: function (data) {
if ($.isEmptyObject(data)) {
$(".validation-summary-errors").append("<li class='postcode-error'>There are no addresses matching your postcode</li>");
$(".validation-summary-errors").removeClass("validation-summary-valid");
}
else {
vm.addresses(data);
addressList.show();
$(".postcode-error").remove();
$(".validation-summary-errors").addClass("validation-summary-valid");
}
}
});
}
});
通过这种方式,您仍然可以在提交表单时获得模型验证消息。