简体   繁体   English

如何取消绑定事件

[英]How to unbind an event

I'm binding a dropdown 我绑定了一个下拉列表

("#dropdownlist").change(function(){
    //Do stuff
});

The above code gets called many times. 上面的代码被多次调用。

How can I unbind this event before binding it each time? 如何在每次绑定之前取消绑定此事件?

您可以使用unbind()方法:

$('#dropdownlist').unbind('change');

您可以使用一个而不是绑定:

("#dropdownlist").one('change',function(){});

Use following logic: 使用以下逻辑:

var f = function () {

}

$('#xx').bind('change', f); // for bind function f
$('#xx').unbind('change', f); // for unbind unbind function f

Additionally jQuery supports namespaces. 另外jQuery支持名称空间。

For example say you have change handlers that do validation and change handlers that do help text hovering. 例如,假设您有更改处理程序,它们执行验证并更改帮助文本悬停的处理程序。

You could register: 你可以注册:

$("#foo").bind("change.validation", doValidation() );

and

$("#foo").bind("change.helptext", toggleHelptext() );

and then you can unbind a specific set before re-adding validation, eg 然后你可以在重新添加验证之前取消绑定特定的集合,例如

$("#foo").unbind("change.validation");
$("#foo").bind("change.validation", doValidation() );

HTH Alex HTH Alex

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

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