weixin_33676492 2016-09-05 15:08 采纳率: 0%
浏览 35

jQuery表单验证Ajax

I have issue with multiple ajax submits. Whenever I click on button submit for every click is one more ajax POST called.

Code bellow

submitHandler: function(form,event) {
            //show popup phonenumber check
            //check captcha on server side
            var fm = $(form);
            $(fm).unbind('submit').submit();
            $("#registration_phone_check").fadeIn();
            var country_number = $("#myDropdown")[0].innerText+fm[0].elements['phone'].value;
            country_number = country_number.replace(/(
|
|)/gm,"");//delete all break lines in string
            $("#phone_check").val(country_number);
            //send code
            $("#check_phone_send_code").click(function(e){
                e.preventDefault();
                var language = "en";

              $("#phone_confirmations").fadeIn();
                    ajax.post('/'+language+'/registration', {
                        email:fm[0].elements['email'].value,
                        uname:fm[0].elements['uname'].value,
                        surname:fm[0].elements['surname'].value,
                        birthday:fm[0].elements['birthday'].value,
                        password:fm[0].elements['password'].value,
                        phone:$("#phone_check").val(),
                        grecaptcha_response:grecaptcha.getResponse()
                    }).done(function(resp) {
                //code when ajax success or some error is returned
                    }).fail(function(resp){
                        grecaptcha.reset();
                    });
                });
        },

So the question is, how can I send ajax only once without multiple ajax POST requests.

  • 写回答

1条回答 默认 最新

  • weixin_33704591 2016-09-05 15:22
    关注

    One way to go would be defining a global variable that locks or unlocks doing the post request. Something like that:

    var submitting = false;
    
    ...
    
    $("#check_phone_send_code").click(function(e){
       if(submitting == false){// check locking variable
         submitting = true; // lock posts
         ajax.post('/'+language+'/registration', {
                        email:fm[0].elements['email'].value,
                        uname:fm[0].elements['uname'].value,
                        surname:fm[0].elements['surname'].value,
                        birthday:fm[0].elements['birthday'].value,
                        password:fm[0].elements['password'].value,
                        phone:$("#phone_check").val(),
                        grecaptcha_response:grecaptcha.getResponse()
                    }).done(function(resp) {
                       submitting = false; // unlock posts
                //code when ajax success or some error is returned
                    }).fail(function(resp){
                        submitting = false;  // unlock posts
                        grecaptcha.reset();
                    });
    
       }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!