简体   繁体   English

首次点击会显示验证错误,并且无效。 第二次点击就可以了

[英]First Click shows validation error and doesn't work. Second click it works fine

I have following JS 我有以下JS

jQuery(document).ready(function() {
    jQuery("#form").validate({

        onfocusout: false,
        onkeyup: false,
        errorPlacement: function (error, element) {
            jQuery(element).tooltipster(tooltipster_config);
            jQuery(element).tooltipster('content', jQuery(error).text());
            jQuery(element).tooltipster('show');
        },

        success: function (label, element) {
            jQuery(element).tooltipster('hide');
        },

        rules: {
            'user': {
                required: true,
                remote: {
                    url: URL,
                    type: "get",
                    async: "false",
                    data: {
                        user: function () {
                            return jQuery("input[name=user]").val();
                        }
                    }
                }

            },
         },

messages: {
    'user': {
        required: 'Enter user ID',
            remote: 'No User exists'
    }
}
});


jQuery("button[id=submit]").click(function (e) {
    e.preventDefault();
    if (jQuery("#form").valid()) {
        jQuery("form").submit();
    }
  });
});

In the above JS i am trying to validate a form field called 'user'. 在上面的JS中,我试图验证一个名为“ user”的表单字段。 If i enter a wrong user id as per the rules defined it displays 'No User exists'. 如果我按照定义的规则输入了错误的用户ID,则会显示“不存在用户”。 But when i correct user id to valid user id, on the first click of submit i still get 'No User exists' error, but on clicking submit second time it works fine. 但是,当我将用户ID更改为有效用户ID时,在第一次单击提交时,我仍然收到“没有用户存在”错误,但是在第二次单击提交时,它可以正常工作。

I also tried 'on' function instead of 'click' but the error still persists. 我还尝试了“ on”功能而不是“ click”,但错误仍然存​​在。 Please let me know where i am getting it wrong. 请让我知道我在哪里弄错了。

Try something like this: 尝试这样的事情:

$("#form").validate({
    rules:
    {
        user:
        {
            required: true,

            // Remote call
            remote: {
                url: 'process.php',  // On the basis of some condition this return either "true" or "false"
                type: 'post',
                data: {
                    user: function() {
                    return $( "#user" ).val();
                    }
                }
            }
        } 
    }
    messages:
    {
        user :
        {
            required: 'Enter user ID',
            remote: 'No User exists'
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 <a href=“..”>第一次点击不起作用,但在FF中第二次点击起作用,在IE中工作正常</a> - <a href=“..”> doesn't work at first click but works at second click in FF, in IE works fine innerHTML 在第一次单击按钮时有效,但在第二次单击时无效 - innerHTML works on first button click but doesn't work on second click 第一次单击元素不起作用,第二次单击起作用 - First click of element doesn't work, second click does javascript中的click()无法正常运行,但在控制台中可以正常运行 - click() in javascript doesn't work but in console it works fine 第一次单击无法初始化我的变量,但第二次单击可以 - Initializing my variable doesn't works by first click, but by second click it works 单击功能不适用于第二次单击 - click function doesn't work for second click iPhone:跨度点击无法正常工作 - iPhone: span click doesn't work fine 苹果浏览器表单验证仅在第二次单击按钮时有效,但在IE 1次单击本身中可以正常工作 - safari form validation works only in second button click, but works fine in IE 1st click itself 为什么移动菜单在第一次点击时不起作用,但在后续点击时可以正常工作? - why mobile menu won't work on first click, but works fine on subsequent clicks? 功能仅在第二次单击时有效,然后可以正常工作吗? - Function only works on second click, then works fine?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM