有很多方法可以做到这一点。例如:
$(".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
如何暂时?在您想再次接受点击事件之前发生了什么? – 2009-12-17 13:42:23
(PS。不允许GET请求执行状态更改。) – bobince 2009-12-17 13:47:20
@bobince:哦,如果我们都遵守了这个简单的规则,那不是很好... @OP:更多关于bob的讨论内容(惊恐地turgid散文)在这里:http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.2 – 2009-12-17 13:51:46