正如我在我的評論中所說的,其中一個選項是以編程方式激活/取消激活任何動作時的控件。當選定的動作完成時(ajax完成,做一些Ajax數據等)激活控件,以便用戶執行下一個動作等。下面是演示我的意思的代碼。
$(function() {
$("#actions").on("click", ".update", function() {
var $this = $(this);
var label = $this.text();
$this.siblings().prop("disabled", true);
$this.text("Please wait...");
//Ajax call
$.ajax({
method: "POST", // or whatever fits
url: "/echo/html/", // your url
data: { method: "methodName" } // whatever data
}).done(function(data) {
//do stuff with returned data
//Enable the controls
$this.siblings().prop("disabled", false);
$this.text(label);
}).fail(function() {
//Handler error related stuff
alert("There was an error!");
//Enable the controls
$this.siblings().prop("disabled", false);
});
});
});
樣品HTML
:
<div id="actions">
<button class="update add">+1</button>
<button class="update subtract">-1</button>
<button class="update delete">Delete</button>
</div>
=Working Demo=
希望有所幫助。
我想你可能正在尋找promise對象:參見['.promise()'](https://api.jquery.com/promise/)。您可能會發現查看['.done()'](https://api.jquery.com/deferred.done/),['.fail()'](https://api.jquery .com/deferred.fail /)和['.always()'](https://api.jquery.com/deferred.always/)方法。 – War10ck
我建議你看看下面的SO文章。 jAndy的回答就是你要找的。 [使用jQuery.queue()隊列ajax請求)(http://stackoverflow.com/questions/4785724/queue-ajax-requests-using-jquery-queue) – MightyLampshade
當與選定的任務相關的任務時,您可能想阻止其他選項選項正在進行中,例如顯示微調或禁用其他選項等。 – lshettyl