2

我正在我的 ASP.NET MVC 4 应用程序中进行客户端验证。我正在尝试从不显眼的 jquery 验证中覆盖默认验证摘要。有一个方法“OnErrors”,我需要覆盖它。我找到了一个解决方案,但对我来说它只适用于单个错误。这是代码:

$(function () {   
var settings = $.data($('form')[0], 'validator').settings;

var oldErrorFunction = settings.errorPlacement;
var oldErrorsFunction = settings.invalidHandler;
var oldSucessFunction = settings.success;


settings.invalidHandler = function (form, validator) {        
    alert("Form is invalid");
    oldErrorsFunction(form, validator);
}

settings.errorPlacement = function (error, inputElement) {
    $(".validation-summary-errors").append("single error ");
    var errorsNumber = 0;
        $(".validation-summary-errors li").each(function () {
            errorsNumber++;        
        });              
    oldErrorFunction(error, inputElement);        
}    
settings.success = function (error) {
    oldSucessFunction(error);
}

});

errorPlacement(针对每个错误)运行良好,但 invalidHandler(针对所有错误)从不触发。

你会怎么处理这个?谢谢

4

1 回答 1

3

我们有这个为我们工作:

form.unbind("invalid-form.validate", form.validate().settings.invalidHandler); // Unbind the existing invalidHandler
form.validate().settings.invalidHandler = $.proxy(customValidationSummary, form); // Replace invalidHandler with a custom one
form.bind("invalid-form.validate", form.validate().settings.invalidHandler); // Rebind the new invalidHandler
于 2013-08-05T15:51:49.753 回答