简体   繁体   English

Unbind onchange而不触发它

[英]Unbind onchange without triggering it

I have some controls each of which updates the others on change. 我有一些控件,每个控件都会在更改时更新其他控件。 To prevent recursion I had thought to unbind the onchange event at the start of the event handler (one handler is shared by the controls concerned) and then rebind it after updating the dependents. 为了防止递归,我曾想过在事件处理程序开始时取消绑定onchange事件(一个处理程序由相关控件共享),然后在更新依赖项后重新绑定它。 All controls involved are tagged with the DateBoundControl class for convenient reference, and I have verified that the expected controls are returned by the jquery expression. 涉及的所有控件都使用DateBoundControl类进行标记,以方便参考,我已经验证了jquery表达式返回了预期的控件。

The problem code looks like this: 问题代码如下所示:

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

However, $(".DateBoundControl").change(); 但是, $(".DateBoundControl").change(); seems to trigger the change event. 似乎触发了变革事件。

Is there something I've missed? 有没有我错过的东西? Is there some flaw in my attempt to unbind the handlers? 我试图解除处理程序的绑定是否存在一些缺陷?

However, $(".DateBoundControl").change(); 但是,$(“。DateBoundControl”)。change(); seems to trigger the change event. 似乎触发了变革事件。

As the documentation clearly states , that's exactly what it's supposed to do. 正如文档明确指出的那样 ,这正是它应该做的事情。

You're looking for the off() method , which unbinds an event. 您正在寻找off()绑定事件的off()方法

Try this : 尝试这个 :

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM