如何使用jQuery删除所有点击事件处理程序? [英] How to remove all click event handlers using jQuery?

查看:102
本文介绍了如何使用jQuery删除所有点击事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了问题。基本上,当用户单击页面上的编辑链接时,将运行以下Jquery代码:

I'm having a problem. Basically, when a user clicks an 'Edit' link on a page, the following Jquery code runs:

$("#saveBtn").click(function () {
    saveQuestion(id);
});

这样做,保存按钮的onClick事件将调用 saveQuestion( )方法并传递单击了编辑链接的问题的ID。

By doing this, the onClick event of the save button calls the saveQuestion() method and passes on the ID of the question for which the 'Edit' link was clicked.

但是如果用户在同一会话中单击编辑2个问题,然后代替覆盖以前的 click 事件处理程序,而是使2个事件处理程序运行,其中一个可能调用 saveQuestion( 1),另一个可能调用 saveQuestion(2)。通过执行此1个问题,另一个问题将被覆盖。

But if in the same session the user clicks edit on 2 questions, then instead of overwriting the previous click event handler, it instead causes 2 event handlers to run, one which might call saveQuestion(1) and the other might call saveQuestion(2). By doing this 1 question overwrites the other.

是否可以删除以前所有的 click 事件

Is there a way to remove all previous click events that have been assigned to a button?

推荐答案

您将使用 off()删除这样的事件:

You would use off() to remove an event like so:

$("#saveBtn").off("click");

但这会删除绑定到此元素的所有点击事件。如果带有SaveQuestion的函数是唯一的事件绑定,那么上面的函数将做到。如果不是,请执行以下操作:

but this will remove all click events bound to this element. If the function with SaveQuestion is the only event bound then the above will do it. If not do the following:

$("#saveBtn").off("click").click(function() { saveQuestion(id); });

这篇关于如何使用jQuery删除所有点击事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆