重写jQuery验证突出显示/取消突出显示方法 [英] Override jQuery validation highlight / unhighlight methods

查看:97
本文介绍了重写jQuery验证突出显示/取消突出显示方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想覆盖jQuery验证程序中的高亮和取消高亮功能以正常执行它们,还向父元素添加一个不同的类.

I want to override the highlight and unhighlight functions in jQuery validator to do what they normally and also add a different class to a parent element.

基本上我想要这样:(在需要帮助的地方,希望有基础..部分)

Basically I want this: (Where I need help wish the base.. part)

    jQuery.validator.setDefaults({
         highlight: function(element, errorClass, validClass) {
             base.highlight(element, errorClass, validClass);
             element.parents('div.form-group').addClass('has-error');
          },
         unhighlight: function(element, errorClass, validClass) {
             base.unhighlight(element, errorClass, validClass);
             element.parents('div.form-group').removeClass('has-error');
          },
    });

这个问题问的是一模一样的东西,但是答案没有.工作.原始函数未传递.也许代码已更改?

This question is asking the exact same thing however that answer doesn't work. The original function is not being passed in. Perhaps the code has since been changed?

推荐答案

highlight: function(element, errorClass, validClass) {  // line 1
    base.highlight(element, errorClass, validClass);    // go to line 1
    ....

您不能在highlight()unhighlight()中调用highlight()unhighlight().为什么?由于任何自定义回调函数都将完全取代插件的内置函数,因此,如果您上面提出的逻辑起作用,它将无限循环.

You can't call highlight() and unhighlight() from within highlight() and unhighlight(). Why? Because any custom callback functions totally over-ride the plugin's built-in functions, so if the logic you propose above worked, it would be infinitely looped.

我想覆盖jQuery验证程序中的高亮和取消高亮功能以正常执行它们,还向父元素添加一个不同的类

I want to override the highlight and unhighlight functions in jQuery validator to do what they normally and also add a different class to a parent element

只需从内部复制默认功能的内容插件插入您的插件,然后根据需要对其进行修改.

Simply copy the meat of the default functions from within the plugin into yours and modify them as you wish.

jQuery.validator.setDefaults({
    highlight: function(element, errorClass, validClass) {
        // default function
        if (element.type === "radio") {
            this.findByName(element.name).addClass(errorClass).removeClass(validClass);
        } else {
            $(element).addClass(errorClass).removeClass(validClass);
        }
        // custom additions
        element.parents('div.form-group').addClass('has-error');
    },
    unhighlight: function(element, errorClass, validClass) {
        // default function
        if (element.type === "radio") {
            this.findByName(element.name).removeClass(errorClass).addClass(validClass);
        } else {
            $(element).removeClass(errorClass).addClass(validClass);
        }
        // custom additions
        element.parents('div.form-group').removeClass('has-error');
    },
});

原始函数未传递.也许代码已更改?

The original function is not being passed in. Perhaps the code has since been changed?

我不知道他们在想什么,或者他们的解决方案是否可行.当您对jQuery Validate插件使用自定义回调函数时,它们将完全覆盖插件中内置的默认函数. (这是jQuery插件的用户可配置选项的工作方式,它们可以替代/替换内置默认值.)

I have no idea what they were thinking or if their solution ever worked. When you use custom callback functions for the jQuery Validate plugin, they completely over-ride the default functions built into the plugin. (This is how user configurable options of jQuery plugins work... they over-ride/replace the built-in defaults.)

因此,只需将插件中的原始代码复制到您的自定义回调函数中,然后根据需要对其进行修改.

So simply copy the original code from the plugin into your custom callback functions and modify them as needed.

这篇关于重写jQuery验证突出显示/取消突出显示方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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