简体   繁体   English

验证完成后,jQuery调用表单ajax提交

[英]jquery call form ajax submission after validation is complete

I have a form and I'm using the j Query validate plugin to validate the form. 我有一个表单,并且正在使用j Query validate插件来验证表单。 I want to call the ajax code to submit the form after the validation piece is complete. 我想在验证完成后调用ajax代码以提交表单。

HOW DO I: 我如何:

// CALL THE postform code if the form validation is complete?
// THIS IS THE FORM VALIDATION CODE
    $(document).ready(function() {
     $("#myForm").validate({
         rules: {
            password: "required",
        password_confirm: {
        equalTo: "#password"
        }                   
        }                
    });
    });


// THIS IS THE AJAX FORM SUBMISSION CODE
// I WANT THIS CODE EXECUTED IF CODE ABOVE IS OK
function postform()
{
  dataString = $("#myForm").serialize();
  $.ajax({
      url: 'https:/www.someothersite.com'+dataString,
      type: 'GET',
      dataType: 'html',
      timeout: 9000,
      error: function(){
          alert('Error posting to other site');
      },
      success: function(html){
          window.location.href="http://www.mysite.com/?action=success";
      }
  });
  return false;
}

It's right there in the documentation . 就在文档中

$("#myForm").validate({
    submitHandler: function(form) {
        postForm();
    }
})

This should work for you. 这应该为您工作。

$(document).ready(function() {

    $('#myForm').validate({
        rules: {
            password: 'required'
        },
        password_confirm: {
            equal_to: 'password'
        },
        submitHandler: function() {
            var dataString = $(this).serialize();

            $.ajax({
                url: 'https://www.someothersite.com' + dataString,
                type: 'GET',
                dataType: 'html',
                timeout: 9000,
                error: function() {
                    alert('Error posting to other site');
                },
                success: function(html) {
                    window.location.href = 'https://www.mysite.com/?action=success';
                }
            });
        }
    });

});

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

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