-1

我已经简化了一点来解释。

如果我有多个依赖选择,让我们以简单的乡镇区域为例。

第一个选择加载国家,第二个选择加载与国家选择值相关的城镇,第三个选择加载与城镇选择值相关的区域。

我不想将异步设置为 false。

$(document).ready(function () {
    // Enumerate select lists
    // Country is already filled

    enumerateChildDropDown("Town", $("Country").val());

    // This shouldnt load until the above has finished
    enumerateChildDropDown("region", $("Town").val());

    // Note there could be many more of these
});

function enumerateChildDropDown(selectId, parentId) {

  $.ajax({
    type: "POST",
    dataType: "json",
    url: "@Url.Action("foo", "foo")",
    data: { parentId: parentId},
  }).done(function (returnedData) {
    // Fill selectId here etc
  }).fail(function (jqXHR, exception) {
    // Some Alert
  });
}

此示例已大大简化,因为实际应用程序会为每个选择加载大量数据,并且具有多达 10 个以上的依赖字段,因此使用了一个 ajax 函数。

我已经研究过使用某种类型的回调,但我发现了不同的 JQuery 版本示例,这让事情有些混乱。我想坚持使用 JQuery,而不是使用任何其他库,即 knockoutjs。

4

2 回答 2

0

您可以使用回调、延迟或承诺。您甚至可以通过转到(默认为 )来同步 jQueryasyncfalseAJAX true

如果你想坚持使用 jQuery 异步,那么我建议你看看这里:http ://api.jquery.com/promise/

从例子:

var div = $( "<div />" );

div.promise().done(function( arg1 ) {
  // will fire right away and alert "true"
  alert( this === div && arg1 === div );
});
于 2013-08-12T17:12:06.020 回答
0

这就是我一直在寻找的东西。

https://github.com/gnarf/jquery-ajaxQueue

于 2014-03-25T20:01:21.847 回答