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

发布于 2024-10-12 02:18:58 字数 1478 浏览 2 评论 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;
    });
}

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

I'm using the jquery form plugin to submit forms to my MVC application. The problem is in my callback "success" callback function. In this function I make 2 calls to other javascript functions that I have. One refreshes part of the page, and the other posts a message to the user on another part of the page.

The problem is, only one of these will execute. It's whichever I put first in the callback function. I've tried to explicitly return a value from the first function to try to get control back to the callback function and it's just not working. I don't want to get rid of my two separate functions because I use this code a lot and I really want to keep it in once place.

Here's the code. (the alert statement is in there just to prove to me that it wasn't working)

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

Here's the additional functions.

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

Each of these functions works perfectly if I place it first in the order. Any idea why I can't run them both?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦一生花开无言 2024-10-19 02:18:59

我猜想某个地方发生了错误..尝试这个来确定。

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

I would guess there is an error occuring somewhere.. Try this to determine.

 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);
}
蓝礼 2024-10-19 02:18:59

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

RefreshGrid is not going returning anything. The $.get() function is async so it is registering a callback and the RefreshGrid is returning from the function before the .get is completed. The get function is returning 0 at a later time but it will not be stored in your variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文