簡體   English   中英

驗證完成后,jQuery調用表單ajax提交

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

我有一個表單,並且正在使用j Query validate插件來驗證表單。 我想在驗證完成后調用ajax代碼以提交表單。

我如何:

// 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;
}

就在文檔中

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

這應該為您工作。

$(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