有很多方法可以做到这一点。例如:
$(".btnRemove").click(function() { var $this = $(this); if ($this.data("executing")) return; $this .data("executing", true) .attr("src", "/url/to/ajax-loader.gif"); $.get("/url/to/django/view/to/remove/item/" + this.id, function(returnedData) { // ... do your stuff ... $this.removeData("executing"); });});要么
$(".btnRemove").click(handler);function handler() { var $this = $(this) .off("click", handler) .attr("src", "/url/to/ajax-loader.gif"); $.get("/url/to/django/view/to/remove/item/" + this.id, function(returnedData) { // ... do your stuff ... $this.click(handler); });}我们还可以使用事件委托来获得更清晰的代码和更好的性能:
$(document).on("click", ".btnRemove:not(.unclickable)", function() { var $this = $(this) .addClass("unclickable") .attr("src", "/url/to/ajax-loader.gif"); $.get("/url/to/django/view/to/remove/item/" + this.id, function(returnedData) { // ... do your stuff ... $this.removeClass("unclickable"); });});- 如果执行完处理程序后不需要重新启用处理程序,则可以使用该
.one()
方法。它绑定只能执行一次的处理程序。请参阅jQuery文档:http - //api.jquery.com/one



