2011-07-05 100 views
1

我已經成功創建了一個無限循環。有人能告訴我如何重寫這個,所以如果ajax請求失敗,表單被提交,所以服務器端驗證可以處理它。jquery - 提交表單上的ajax錯誤?

$('form#loginForm').submit(function(evt) { 
    evt.preventDefault(); 

    // We rely on HTML5 validation for the validation - this checks if the account is valid. 

    $.ajax({ 
     type: "POST", 
     url: "some.php", 
     error: function(msg) { 
      alert(1); 
      $('#loginForm').submit(); 
     } 
    }); 
}); 

回答

6

從你的代碼中,當你調用error函數提交時,你調用的是和ajax調用相同的函數。如果你只是想提交表單而不做這個Ajax調用,你需要重新提交submit函數;

$('form#loginForm').submit(function(evt) { 
    evt.preventDefault(); 

    // We rely on HTML5 validation for the validation - this checks if the account is valid. 

    $.ajax({ 
     type: "POST", 
     url: "some.php", 
     error: function(msg) { 
      alert(1); 
      //rebind submit function to not do ajax call 
      $('#loginForm').submit(new function(){return true;}); 
      //then call submit 
      $('#loginForm').submit(); 
     } 
    }); 
});