簡體   English   中英

SubmitHandler表單中的Ajax提交不起作用

[英]Ajax inside submitHandler form submit not working

我正在注冊用戶,

我使用jquery驗證插件驗證字段。

在“ submitHandler”中,我調用了Ajax,然后,如果響應正常,則提交表單....但是未提交表單。但是我收到了“確定”的警報。

這是我的代碼,

jQuery(document).ready(function(){
// Setup form validation on the #register-form element
jQuery("#reg_form").validate({
// Specify the validation rules
errorElement: "span",
rules: {
    fname:  {
    required: true,
    maxlength: 12,
    minlength: 4,
    remote: {url: "<?php echo get_template_directory_uri() ?>/ajax-checkusername.php", type : "post"}
    },
    lname: "required",
    phone: {required:true,number: true},
    address: "required",
    email: {
    email: true,
    required: true,
    remote: {url: "<?php echo get_template_directory_uri() ?>/ajax-checkemail.php", type : "post"}
    },
    password: "required",
    cpassword: {
    required: true,
    equalTo: "#password"
    },
    profile_picture: {
    accept: "image/*"
    }
},
// Specify the validation error messages
messages: {
    fname:  {
    email: "This field is required.",
    minlength: "Please enter atleast 4 characters",
    maxlength: "Too much characters",
    remote: "username already in use!",
    },
    lname: "This field is required.",
    phone: {
    required: "This field is required",
    number: "Please Enter Number"
    },
    address: "This field is required.",
    email: {
    required: "Please Enter Email!",
    email: "This is not a valid email!",
    remote: "Email already in use!",
    },
    password: "This field is required.",
    cpassword :{
    required:"This field is required.",
    equalTo:"Password Not matched."
    },
    profile_picture: {
    accept:"Please Insert Image"
    }
},
submitHandler: function(form) { 
    var frm = $(form);alert(frm);

    if (jQuery("#g-recaptcha-response").val()) {

        jQuery.ajax({
            type: 'POST',
            url: "<?php echo get_template_directory_uri(); ?>/ajax_verify_captcha.php", // The file we're making the request to
            dataType: 'html',
            async: true,
            data: {
                captchaResponse: jQuery("#g-recaptcha-response").val()
            },
            success: function (data) {
                alert(data); 

                //jQuery('#reg_form').get(0).submit();
                if(data=="OK"){
                    frm.submit();

                }

                //alert("everything looks ok");
              // jQuery('#reg_form')[0].submit(); 

            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("You're a bot");
                alert(XMLHttpRequest);
                return false;
            }

        });


    } else {
        alert("Please fill the captcha!");
        return false;
    }


}
});
});

我要檢查的第一件事是if語句中的值。 此外,您可以只使用(數據)來查看它是否已定義,或者使用.length來查看它是否被設置為任何值。

console.log(data=="OK");
if(data.length){
  frm.submit();
}

您可能應該返回一個布爾值或一個數字,並使用parseInt()來確保比較兩個相同數據類型的變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM