jQuery仅验证远程onblur,但允许onkeyup休息 [英] jQuery Validate remote onblur only but allow onkeyup for rest

查看:95
本文介绍了jQuery仅验证远程onblur,但允许onkeyup休息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行jquery远程验证,以查看名称是否唯一,但是我不想在每个onkekup事件上进行远程验证,但是我想在blur事件上执行此操作(当用户使用时)离开文本框).但是使用下面的当前代码,按下第二个字符后会触发.我想继续在onkeyup上触发其余的规则,例如required和minlength以及其他元素的规则. 没有仅用于单个规则的属性来控制此行为吗?我注意到设置了适用于整个表格的默认设置.

  elem.validate({
        ignore: "",
        rules: {
            name: {
                required: true,
                minlength: 2,
                maxlength: 60,
                remote: {
                    url: "/api/IsUniqueName",
                    onkeyup: false,
                    type: "get",
                    contentType: "application/json",
                    data: {
                        name: function () {

                            return elem.find('input[name^=Name]').val();
                        }
                    },
                    headers: {
                        RequestVerificationToken: Indexreqtoken
                    },
                }
            },
            ...

解决方案

您不能将onkeyup选项放在remote规则之内……这不是remote方法的工作原理. remote方法只能接受与jQuery .ajax()相同的选项,而不能接受其他任何选择. /p>

但是,您不能在每个规则"的基础上限制或控制触发事件.这些事件是针对整个表单或在每个字段中单独捕获的,不能仅限于特定的事件规则.

如果要将插件的onkeyup函数限制为某些字段,则可以在onkeyup选项中使用条件...

$('#myForm').validate({
    onkeyup: function(element, event) {
        if ($(element).attr('name') == "name") {
            return false; // disable onkeyup for your element named as "name"
        } else { // else use the default on everything else
            if ( event.which === 9 && this.elementValue( element ) === "" ) {
                return;
            } else if ( element.name in this.submitted || element === this.lastElement ) {
                this.element( element );
            }
        }
    },
    ignore: [],  // <- note the proper format for the "ignore nothing" setting.
    rules: {
        name: {
            required: true,
            minlength: 2,
            maxlength: 60,
            remote: {
                url: "/api/IsUniqueName",
                ....


引用OP :

仅针对单个规则,没有属性可以控制此行为吗?"

否,不能基于每个规则"控制触发事件.如上所示,只能针对整个表单或特定字段进行控制.

https://stackoverflow.com/a/21313848/594235

I am trying to do a jquery remote validation to see if a name is unique or not, but i do not want to do remote validation on every single onkekup event, however i would like to do this on blur event(when user leaves the textbox). but with current code i have below, it fires up after 2nd character is pressed. i would like to continue to have rest of the rules fire on onkeyup like required and minlength and rules for other elements. is there not a property to control this behavior, just for single rule? i noticed a set default that does for entire form.

  elem.validate({
        ignore: "",
        rules: {
            name: {
                required: true,
                minlength: 2,
                maxlength: 60,
                remote: {
                    url: "/api/IsUniqueName",
                    onkeyup: false,
                    type: "get",
                    contentType: "application/json",
                    data: {
                        name: function () {

                            return elem.find('input[name^=Name]').val();
                        }
                    },
                    headers: {
                        RequestVerificationToken: Indexreqtoken
                    },
                }
            },
            ...

解决方案

You cannot put the onkeyup option inside of the remote rule... that's not how the remote method works. The remote method can only accept the same options as jQuery .ajax() and nothing else.

However, you cannot restrict or control the triggering events on a "per rule" basis. These events are captured for the whole form or individually on each field, they can not be confined to a specific rule.

If you want to restrict the plugin's onkeyup function to certain fields, then you would use a conditional within the onkeyup option...

$('#myForm').validate({
    onkeyup: function(element, event) {
        if ($(element).attr('name') == "name") {
            return false; // disable onkeyup for your element named as "name"
        } else { // else use the default on everything else
            if ( event.which === 9 && this.elementValue( element ) === "" ) {
                return;
            } else if ( element.name in this.submitted || element === this.lastElement ) {
                this.element( element );
            }
        }
    },
    ignore: [],  // <- note the proper format for the "ignore nothing" setting.
    rules: {
        name: {
            required: true,
            minlength: 2,
            maxlength: 60,
            remote: {
                url: "/api/IsUniqueName",
                ....


EDIT:

Quote OP:

"is there not a property to control this behavior, just for single rule?"

No, the triggering events cannot be controlled on a "per rule" basis. They can only be controlled for the whole form OR for a specific field, as I've shown above.

https://stackoverflow.com/a/21313848/594235

这篇关于jQuery仅验证远程onblur,但允许onkeyup休息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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