JQuery ajax表单提交工作时调试,但不能没有



我有一个使用ajax加载到页面上的表单。然后使用malsup jquery表单插件提交表单。

奇怪的是,当我在此方法中添加firebug断点行或警报时,表单可以工作,但是当我删除警报或调试时,提交代码永远不会运行。

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)。准备好了,这没什么区别。

任何想法?

可能你需要在加载完成后调用函数的后面部分,试试这个

 $(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; 
        });
    }
    });

相关内容

最新更新