jQuery.validate.js onkeyup = true 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15103289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:22:25  来源:igfitidea点击:

jQuery.validate.js onkeyup = true error

jqueryjquery-validate

提问by Adam Levitt

With my jquery validation configuration, I get the following error when setting onkeyup to true. It works if I set it to false, but I don't get validation feedback until I blur the field, and I'm looking to get validation on each keyup:

使用我的 jquery 验证配置,将 onkeyup 设置为 true 时出现以下错误。如果我将它设置为 false,它会起作用,但是在我模糊字段之前我不会得到验证反馈,并且我希望在每个键上得到验证:

$("#signupForm").validate({
        onkeyup: true,        
        onclick: false
        // rules omitted for brevity
    }

I get the following error:

我收到以下错误:

TypeError: validator.settings[eventType].call is not a function
validator.settings[eventType].call(validator, this[0], event);
jquery.validate.js line 391

回答by Sudhir Bastakoti

You actually need to set the onkeyupoption to a function that accepts the element being validated as a parameter, so try changing:

您实际上需要将该onkeyup选项设置为接受作为参数进行验证的元素的函数,因此请尝试更改:

onkeyup: true, 

to

onkeyup: function(element) {$(element).valid()}

回答by Sparky

onkeyupis enabled by default so you do not need to set it to true. If you do, you break the functionality already built into the plugin.

onkeyup默认情况下启用,因此您无需将其设置为true. 如果这样做,就会破坏插件中已经内置的功能。

You have three options:

您有三个选择:



1)Leave the onkeyupoption out of .validate(). This keeps onkeyupfunctionality enabledby default. (edit: "by default" means that validation occurs on every "key-up" event only afterthe field is initially validated by another event.)

1)onkeyup选项排除在.validate(). 这会保持默认启用onkeyup功能。(编辑:“默认”意味着只有在该字段最初由另一个事件验证,才会在每个“按键”事件上进行验证。)

DEMO: http://jsfiddle.net/ZvvTa/

演示http: //jsfiddle.net/ZvvTa/



2)onkeyupcan be set to falseto disablethis option.

2)onkeyup可以设置false禁用此选项。

DEMO: http://jsfiddle.net/ZvvTa/1/

演示http: //jsfiddle.net/ZvvTa/1/



3)Replace onkeyupwith your own callback function to modifyhow it operates. (Demo uses default function)

3)替换onkeyup为您自己的回调函数以修改其操作方式。(Demo使用默认功能)

DEMO: http://jsfiddle.net/ZvvTa/2/

演示http: //jsfiddle.net/ZvvTa/2/

Below is the default, unmodified, onkeyupcallback function:

下面是默认的未修改onkeyup回调函数:

onkeyup: function( element, event ) {
    if ( event.which === 9 && this.elementValue(element) === "" ) {
        return;
    } else if ( element.name in this.submitted || element === this.lastElement ) {
        this.element(element);
    }
}


See: http://docs.jquery.com/Plugins/Validation/validate#toptions

请参阅:http: //docs.jquery.com/Plugins/Validation/validate#toptions



EDIT:

编辑

By default, the plugin does not do any "key-up" validation until afterthe field is initially validated by another event. ("Lazy" validation)

默认情况下,该插件该字段最初被另一个事件验证之前不会进行任何“key-up”验证。 (“懒惰”验证)

So here is a more properly modified version of the onkeyupcallback function that will provide immediateonkeyupvalidation. ("Eager" validation)

所以这里是onkeyup回调函数的更正确修改版本,它将提供即时onkeyup验证。 (“渴望”验证)

DEMO: http://jsfiddle.net/QfKk7/

演示:http: //jsfiddle.net/QfKk7/

onkeyup: function (element, event) {
    if (event.which === 9 && this.elementValue(element) === "") {
        return;
    } else {
        this.element(element);
    }
}

回答by Arun P Johny

@Sparky is right, The correct answer is to remove the onkeyupoption, my fiddle was working because I removed the onkeyupoption not because I changed it to keyup.

@Sparky 是对的,正确答案是删除该onkeyup选项,我的小提琴正在工作是因为我删除了该onkeyup选项,而不是因为我将其更改为keyup.

By default the validation will happen on keyup event, if you want to turn off this feature then you have to add the setting onkeyup: false.

默认情况下,验证将发生在 keyup 事件上,如果您想关闭此功能,则必须添加设置onkeyup: false

See the updated demo.

请参阅更新的演示。

Demo: Fiddle

演示:小提琴

回答by Anusha

boolean values are not valid values for onkeyup event. Instead add a function, on what is needed to be done. Try something like:

布尔值不是 onkeyup 事件的有效值。而是添加一个函数,关于需要做什么。尝试类似:

    onkeyup: function(element) {
           $(element).valid(); 
    }
    onblur: function(element) { 
         $(element).valid(); 
    }

回答by Artur K?dzior

How about extending onkeyup:

如何扩展 onkeyup:

    $('#signup-form').validate({

        onkeyup: function(element) {
            var element_id = $(element).attr('id');
            if (this.settings.rules[element_id].onkeyup !== false) {
                $(element).valid();
            }
        },

So now within rules you can do this:

所以现在在规则内你可以这样做:

        rules: {
            username: {
                required: true,
                minlength: 3,
                maxlength: 25,
                onkeyup: false, /* on blur validation */
            },
            password: {
                required: true,                   
                passwordmeter: true,
                onkeyup: true, /* on keyup validation */
            },

By extending onkeyupwe made it a default behaviour, so sticking onkeyup: trueis optional.

通过扩展onkeyup我们使它成为默认行为,所以坚持onkeyup: true是可选的。