javascript回调函数 - 多个函数不执行

时间:2011-01-13 21:10:57

标签: javascript jquery callback jquery-callback

我正在使用jquery表单插件将表单提交给我的MVC应用程序。问题出在我的回调“成功”回调函数中。在这个函数中,我对其他javascript函数进行了2次调用。一个刷新部分页面,另一个在页面的另一部分向用户发布消息。

问题是,其中只有一个会执行。这是我在回调函数中放置的第一个。我试图从第一个函数显式返回一个值,试图让控制回到回调函数,它只是不起作用。我不想摆脱我的两个独立的功能,因为我经常使用这个代码,我真的想把它保存在一个地方。

这是代码。 (提醒声明就在那里,只是为了向我证明它不起作用)

 function ShowProposedDate_Submit() {
    // prepare Options Object 
    var options = {
        dataType: 'json',
        success: function (responseText) {
            $("#blankModal").dialog("close");
            var ret = RefreshGrid(); //this function will execute but will not return
            alert(ret); //this statement won't execute
            PostMessage(responseText.msg); //this function won't execute



        },
        error: function (responseText) {
            $("#feedback").html(responseText.msg);
        }
    };

    // pass options to ajaxForm 
    $('#showProposedDate').ajaxForm(options);
}

这是附加功能。

 function PostMessage(message) {
    $('.message_wrap').remove();
    $("<div></div>)").addClass("message_wrap")
                 .text(message)
                 .prependTo(".grid_20");
    KillMessage();
}

function RefreshGrid() {
    $.get("/workitems/list", function (data) {
        $("#grid").replaceWith(data);
        getAvailableActions(0);
        getMoreDetails(0);
        ResetCheckbox();
        return 0;
    });
}

如果我将它放在第一顺序中,这些功能中的每一个都能完美运行。知道为什么我不能同时运行它们吗?

2 个答案:

答案 0 :(得分:0)

RefreshGrid不会返回任何内容。 $ .get()函数是异步的,因此它正在注册一个回调,而RefreshGrid在.get完成之前从函数返回。 get函数稍后返回0,但不会存储在变量中。

答案 1 :(得分:0)

我猜想某处发生了错误。请尝试此确定。

 function ShowProposedDate_Submit() {
    // prepare Options Object 
    var options = {
        dataType: 'json',
        success: function (responseText) {
            $("#blankModal").dialog("close");
            try { RefreshGrid(); } catch ( err ) { alert("Error: " + err); }
            try { PostMessage(responseText.msg); } catch ( err ) { alert("Error2: " + err); }



        },
        error: function (responseText) {
            $("#feedback").html(responseText.msg);
        }
    };

    // pass options to ajaxForm 
    $('#showProposedDate').ajaxForm(options);
}
相关问题