覆盖jQuery验证MVC4中的默认设置 [英] Override jQuery validate default settings in MVC4

查看:108
本文介绍了覆盖jQuery验证MVC4中的默认设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要覆盖查询验证插件,请在普通文档中,推荐方式是:

To override the Query validate plugin, in the plaugsin document, the recommend way is:

$(".selector").validate({
    invalidHandler: function(form, validator) {
      var errors = validator.numberOfInvalids();
      if (errors) {
        var message = errors == 1
          ? 'You missed 1 field. It has been highlighted'
          : 'You missed ' + errors + ' fields. They have been highlighted';
        $("div.error span").html(message);
        $("div.error").show();
      } else {
        $("div.error").hide();
      }
    }
})

但是,它在带有jquery-1.7.1.js的MVC4中不起作用.看来jquery.validate.unobtrusive.js阻止了覆盖句柄被调用.如果不包括此文件,则会调用覆盖句柄并显示消息.有人遇到同样的问题吗?

However, it does not work in MVC4 with jquery-1.7.1.js. it seems jquery.validate.unobtrusive.js prevent the override handle to be called. If do not include this file, the override handle is called and message is displayed. Anybody have the same issue?

推荐答案

我终于按照布鲁斯.诀窍是先删除旧的处理程序,再连接您的自定义处理程序.您可以用相同的方法覆盖其他设置. Microsoft应该加快修复其jquery.validate.unobtrusive.js使其与jquery.validate一起使用的速度.据报道不引人注目的[version ="2.0.20710.0"]会破坏jquery-1.9.0 .

I finally make it work as suggested by Bruce. The trick is to remove the old handler before hookup your custom one. You can override other settings the same way. Microsoft should speed up to fix its jquery.validate.unobtrusive.js to work with jquery.validate. It has been reported unobtrusive [version="2.0.20710.0"] breaks jquery-1.9.0.

$('form').each(function() {
    $(this).unbind("invalid-form.validate"); // remove old handler!!!!
    $(this).bind("invalid-form.validate", function(e, validator) {
        //alert("ok");
        var errors = validator.numberOfInvalids();
        if (errors) {
            var message = errors == 1
                ? 'You missed 1 field. It has been highlighted'
                : 'You missed ' + errors + ' fields.  They have been highlighted';
            $("div.error span").html(message);
            $("div.error").show();
        } else {
            $("div.error").hide();
        }
    });
});

这篇关于覆盖jQuery验证MVC4中的默认设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆