如何从另一个onclick事件中停止/暂停jquery更改事件的执行

How to stop/pause the execution of a jquery change event from a another onclick event?

本文关键字:事件 jquery 暂停 执行 另一个 onclick      更新时间:2023-09-26

我有一个jQuery更改事件,在文本框被更改后触发,
该事件仅在文本框失去焦点时触发。
如果用户的下一件事是按取消按钮,那么改变事件被触发,然后取消按钮的onclick也被触发,但我想"杀死"变化事件,我想从onclick事件。
我知道在jQuery中对事件有某种控制。

<input id="change" type="text" />
<input id="cancel" type="button" value="Cancel" onclick="return cancelEvent(this);" />

$(function () 
{
   $('#change').live('change', function() {
    alert('changed');
   });
});
function cancelEvent(e){
    //kill the change event and continue
    alert('cancel');
    return false;
}
有一个解决方案,如果我杀死然后重新绑定事件:

$('#changeButton').die('change',..).live('change',...);

但它必须是一个更有效的解决方案。

这里有一个jsbin链接,你可以在那里玩它!
希望这更容易理解。
欢迎对这个问题有任何贡献

编辑#1

似乎我现在明白你想要什么,所以忽略我之前的回答。相反,看看这个例子。保存焦点时控件的值。如果按下取消按钮,则恢复该值,即使已提交更改。

<标题>编辑# 2 h1> 样的例子,少了一个事件处理程序