0

我正在使用 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;
    });
}

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

4

2 回答 2

0

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

于 2011-01-13T21:16:22.897 回答
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);
}
于 2011-01-13T21:16:47.920 回答