2011-01-13 125 views
0

我正在使用jQuery表單插件來提交表單到我的MVC應用程序。問題出在我的回調「成功」回調函數中。在這個函數中,我對其他的JavaScript函數進行了2次調用。一個刷新頁面的一部分,另一個在頁面的另一部分向用戶發送消息。javascript回調函數 - 多個函數不執行

問題是,只有其中一個會執行。這是我首先在回調函數中放置的。我試圖顯式地從第一個函數中返回一個值,試圖讓控制回到回調函數,但它不工作。我不想擺脫我的兩個單獨的功能,因爲我使用這些代碼很多,我真的想保留它在一次。

這是代碼。 (警報語句是在那裏只是爲了證明給我看,這是不工作)

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

如果我將這些函數中的第一個按順序排列,那麼每個函數都可以很好地工作。任何想法,爲什麼我不能運行它們兩個?

回答

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

是的。好決定。這有助於追蹤它。這兩個函數現在都被調用,不需要返回值。 – BZink 2011-01-13 21:35:18

0

RefreshGrid不會返回任何東西。 $ .get()函數是異步的,因此它正在註冊一個回調函數,並且RefreshGrid在.get完成之前從函數返回。 get函數稍後返回0,但它不會存儲在變量中。