2015-10-15 59 views
-1

表單提交時出現「遞歸太多」的錯誤,這裏是我的代碼任何人都可以告訴我什麼是問題?在jquery中提交表單時遞歸錯誤太多

$("#ajax-post-form").on('beforeSubmit', function (e) { 
      var postType = $("#post-posttype").val(); 
      var postVideo = $("#post-postvideo").val(); 
      var postImages = $("#post-postimages").val(); 
      if(postVideo == '' && postType == 3) { 
       alert("Please upload video"); 
       return false; 
      } 
      else if(postType == '1' || postType == '2') { 
       if(postType == '2' && postImages == '') { 
        alert("Please upload image"); 
        return false; 
       } else { 
        $("#spinnerLoader").show(); 
        $("#ajax-post-form").submit(); 
        return true; 
       } 

      } 

回答

0

冷凝下來到最低工作示例...

$("#ajax-post-form").on('beforeSubmit', function (e) {    
    $("#ajax-post-form").submit(); //triggers 'beforeSubmit' event and causes recursion 
}) 

您可能希望在試圖重新提交之前解開你beforeSubmit處理...

...或添加標誌以指示重新提交:

var resubmit = false; 
$("#ajax-post-form").on('beforeSubmit', function (e) {  
    if(resubmit)return; 
    resubmit = true; 
    $("#ajax-post-form").submit(); //this triggers 'beforeSubmit' event 
    resubmit = false; 
})