jQuery keyup检查并单击以激活ajax

时间:2012-11-26 16:48:19

标签: jquery jquery-click-event

我有一个输入,在输入每个字符后检查密码。这会在每个密钥上添加POST检查。

确认密码检查后,启用提交按钮,当您单击此按钮时,它会通过ajax将详细信息发送到php。但它实际上发射的次数与按键检查的次数相同。有没有办法限制只开火一次?如果我的密码中有6个字符,那么它的密钥检查完成6次,点击更新激活6次。

密钥密码检查

$('input[name=currentpassword]').keyup(function(){
    var currentcheck = $(this).val();
    var dataString='thisConfirm='+ currentcheck +'&userid='+$('input[name=userid]').val();
    setTimeout(function(){
        PasswordChecking =  $.ajax({
                                type:"POST",
                                url:"/assets/inc/password-check.php",
                                data:dataString,
                                dataType:'html',
                                context:document.body,
                                global:false,
                                async:true,
                                success:function(data){
                                    //return data
                                    var PasswordChecking="1";
                                    console.log(PasswordChecking);
                                    console.log("here you would decide on data for valiadation success");
                                    checkconfirms(emailconfirm, passconfirm, PasswordChecking);

                                }
                            }).responseText;
    }, 1000);

});

jQuery Update Button位于checkconfirms函数内

$('input[name=updatedetails]').click(function(){
                    var newEmailValue = $('input[name=newemailconfirm]').val();
                    var accountID   =   $('input[name=userid]').val();              
                    var dataString='email='+ newEmailValue +'&userid='+accountID;
                    $.ajax({
                            type:"POST",
                            url:"/assets/inc/user-change-email.php",
                            data:dataString,
                            dataType:'html',
                            context:document.body,
                            global:false,
                            async:false,
                            success:function(data){
                                var overlay = $("<div/>").addClass("overlay");
                                var logindialog = $("<div/>").addClass("popup-login");
                                $("body").append(overlay);
                                $("body").append(logindialog);
                                $(".popup-login").prepend('<p><label for="email">Email</label><input type="text" name="reloginemail" id="email"></p><p><label for="password">Password</label><input type="password" name="reloginpass" id="password"></p><input type="button" name="relogindude" value="sign in">');
                                $(".overlay").fadeIn();
                                $(".popup-login").fadeIn();
                                // Once the pop up is created this enable the click function which will enable the ajax form check.
                                $('input[name=relogindude]').click(function(){
                                    var email = $('input[name=reloginemail]').val();
                                    var password = $('input[name=reloginpass]').val();              
                                    var dataStringed='newemail='+ email +'&newpassword='+password;
                                    $.ajax({
                                        type:"POST",
                                        url:"/assets/inc/login.php",
                                        data:dataStringed,
                                        success:function(data){
                                            window.location.reload();
                                        }
                                    });
                                });//End relogindude click function
                            }
                    });//End updatedetails click function

2 个答案:

答案 0 :(得分:2)

尝试此操作,清除超时以限制请求数量:

var timeout;
$('input[name=currentpassword]').keyup(function(){
    var currentcheck = $(this).val();
    var dataString='thisConfirm='+ currentcheck +'&userid='+$('input[name=userid]').val();
    if(typeof timeout != 'undefined') clearTimeout(timeout);
    timeout = setTimeout(function(){
        PasswordChecking =  $.ajax({
                                type:"POST",
                                url:"/assets/inc/password-check.php",
                                data:dataString,
                                dataType:'html',
                                context:document.body,
                                global:false,
                                async:true,
                                success:function(data){
                                    //return data
                                    var PasswordChecking="1";
                                    console.log(PasswordChecking);
                                    console.log("here you would decide on data for valiadation success");
                                    checkconfirms(emailconfirm, passconfirm, PasswordChecking);

                                }
                            }).responseText;
    }, 1000);

答案 1 :(得分:0)

除了你想要做什么是一个好主意,如果你想停止触发超时(当按下另一个键时),那么你需要存储由{{1}返回的超时ID }。然后,您就可以致电setTimeout取消它。

像这样的片段:

clearTimeout

现在如果在超时之前发生了另一个密钥启动,则不会为之前的密钥调用超时。