简体   繁体   English

替代功能

[英]Alternative for keyup function

am working in a popup when we click login a popup opens with fields !!! 当我们单击登录时,我正在弹出窗口中工作! i want to check username and password with database before login button fired.. used ajax with keyup function for password field! 我想在登录按钮被触发之前使用数据库检查用户名和密码。用过带密码功能的Ajax!

but keyup is firing for every character typed in password field... but the requirement is after typing password field ajax should be called and result should be displayed... is there any alternative for keyup? 但是会为在密码字段中键入的每个字符触发keyup ...但要求是在输入密码字段后应调用ajax并显示结果... keyup可以替代吗?

now am getting as "wrong password for every character typed" and after entring correct password it will login,... but i want to show error as wrong password when user completely enters password (if password is wrong) 现在得到的是“输入的每个字符错误的密码”,输入正确的密码后它将登录,但是我想在用户完全输入密码时将错误显示为错误的密码(如果密码错误)

<script type="text/javascript">
    $(document).ready(function() {
       $("#upwd").change(function()
       //$('#upwd').on('input', function() 
       //$('#upwd').keyup(_.debounce(doSomething , 500), function() {
       var upwd = $("#upwd").val();
       var uemail = $("#uemail").val();
       var msgbox = $("#status");
       //alert(uemail);
       //alert(upwd);
       //setTimeout(function() { 
       //if($('#upwd').val() == upwd){ 
       $.ajax({ 
           type: "POST",
           url: "checkl_login.php",
           data: "uemail="+ uemail,
           success: function(msg){
           $("#status").ajaxComplete(function(event, request){
           if(msg == 'OK') {
             msgbox.html(msg);
             //return false;
           } else {
             msgbox.html(msg); 
           }
       });
    }
   });
     return false;
   });

  });

</script>

error displayed in status id in span...... 在跨度的状态ID中显示错误...

checkl_login.php code is aalso perfect..... checkl_login.php代码也很完美.....

Try using .focusout() event as shown :- 尝试使用.focusout()事件,如下所示:

$("#upwd").on("focusout",function(){
 //..your code..
});

and in addition of these events ,use a login button also and check validations on the click of the button. 除了这些事件外,还请使用登录按钮,并单击按钮检查验证。

使用change事件代替keyup

IMHO it's better: 恕我直言,更好:

  1. to use keyup (it allows to handle any keyboard changes. For example, removing the symbol by backspace) 使用keyup(它可以处理任何键盘更改。例如,通过退格键删除符号)
  2. to handle 'paste' (because user may copy/paste password but not type it) 处理“粘贴”(因为用户可以复制/粘贴密码但不能输入密码)
  3. to validate password if user does not press a key during some period of time (for example, within 1 second) - setTimeout and clearTimeout should help 如果用户在一段时间(例如1秒钟内)内未按任何键,则可以验证密码-setTimeout和clearTimeout应该会有所帮助
  4. to abort ajax request if user starts to type when ajax request is in progress 如果用户在进行ajax请求时开始输入内容,则中止ajax请求

Ie you may try something like the following: 也就是说,您可以尝试以下操作:

$(document).ready( function() {

    var ajax_h;
    var delayMilliseconds = 1000; // i.e. = 1 second
    var timeoutId;

    function checkAuth() {
        if (ajax_h) {
            ajax_h.abort();
        }
        ajax_h = $.ajax(
            // PUT YOUR CODE HERE
        );
    }

    $("#upwd").on('keyup paste', function () {
        if (timeoutId) {
            clearTimeout(timeoutId);
        }
        timeoutId = setTimeout(checkAuth, delayMilliseconds);
    });

});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM