取消绑定onchange而不触发它

Unbind onchange without triggering it

本文关键字:绑定 onchange 取消      更新时间:2023-09-26

我有一些控件,每个控件在更改时更新其他控件。为了防止递归,我曾考虑在事件处理程序开始时取消绑定onchange事件(一个处理程序由相关控件共享),然后在更新依赖项后重新绑定它。为了方便引用,所有涉及的控件都用DateBoundControl类标记,并且我已经验证了预期的控件是由jquery表达式返回的。

问题代码如下:

function onDateBoundChange(evt) {
  $(".DateBoundControl").change();
  //do stuff to other controls that use this change handler
  $(".DateBoundControl").change(onDateBoundChange);
}

然而,$(".DateBoundControl").change();似乎触发变化事件。

我错过了什么吗?我解绑定处理程序的过程中有什么缺陷吗?

试试这个:

function onDateBoundChange(evt) {
  $(".DateBoundControl").unbind('change', onDateBoundChange);
  //do stuff to other controls that use this change handler
  $(".DateBoundControl").bind('change', onDateBoundChange);
}
然而,

$(".DateBoundControl").change ();似乎触发了更改事件。

正如文档明确指出的那样,这正是它应该做的。

您正在寻找off()方法,该方法解除绑定事件。