2011-09-09 22 views
0

我有一个使用ajax加载到页面的表单。然后使用malsup jquery表单插件提交表单。JQuery ajax表单在调试时提交,但不是没有

奇怪的是,当我在此方法中添加一个萤火虫断点行或警报时,表单很有效,但是当我删除警报或调试时,提交代码从不运行。

function addAttachment(attachmentType, path){ 
var typeSplit = attachmentType.split(":"); 
if(path == null){ 
    path = ""; 
} 
var url = "/add/" + typeSplit[0] + "/" + typeSplit[1];     
addOverlayDivs(); //adds div to load the form into 
// load the form 
var snippet = $('#overlay').load(url, function(response, status, xhr) { 
    if (status == "error") { 
     var msg = "Sorry but there was an error: "; 
     $("#overlay").html(msg + xhr.status + " " + xhr.statusText); 
    } 
}); 
var prefix = typeSplit[0]; 
var type = typeSplit[1]; 
//this alert will cause the submit form to work 
alert("bind overlay called");//if I comment this out the formsubmit doesn't work 

var options = { 
     target: null, // target element(s) to be updated with server response 
     beforeSubmit: showRequest, 
     success: showResponse, 
     url: "/add/" + prefix + "/" + type, 
     type:  "POST", 
     dataType: "json" 
}; 
$('#overlayForm').submit(function() { 
    $(this).ajaxSubmit(options); 
    // always return false to prevent standard browser submit and page navigation 
    return false; 
});} 

我试过了,没有使用$(document).ready,并没有什么区别。

任何想法?

+0

您应该添加您的解决方案作为一个答案,自己接受它,而不是将它添加到的问题。 – Matt

回答

0

可能是你需要打电话给你的功能后部分负荷结束后,试试这个

$(document).ready(function(){ 

     function addAttachment(attachmentType, path){ 
     var typeSplit = attachmentType.split(":"); 
     if(path == null){ 
      path = ""; 
     } 
     var url = "/add/" + typeSplit[0] + "/" + typeSplit[1];     
     addOverlayDivs(); //adds div to load the form into 
     // load the form 
     var snippet = $('#overlay').load(url, function(response, status, xhr) { 
      if (status == "error") { 
       var msg = "Sorry but there was an error: "; 
       $("#overlay").html(msg + xhr.status + " " + xhr.statusText); 

      } 
        Dowork();//function call after load complete 
     }); 
    } 


    function Dowork(){ 
      var prefix = typeSplit[0]; 
     var type = typeSplit[1]; 
     //this alert will cause the submit form to work 


     var options = { 
       target: null, // target element(s) to be updated with server response 
       beforeSubmit: showRequest, 
       success: showResponse, 
       url: "/add/" + prefix + "/" + type, 
       type:  "POST", 
       dataType: "json" 
     }; 
     $('#overlayForm').submit(function() { 
      $(this).ajaxSubmit(options); 
      // always return false to prevent standard browser submit and page navigation 
      return false; 
     }); 
    } 
    }); 
+0

就是这样......谢谢 – Lee