最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to unbind an event - Stack Overflow

programmeradmin8浏览0评论

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?

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?

Share Improve this question edited Oct 5, 2011 at 19:15 yoozer8 7,4897 gold badges62 silver badges96 bronze badges asked Oct 2, 2009 at 10:32 SanthoshSanthosh 20.4k23 gold badges66 silver badges76 bronze badges 1
  • I'm not sure what you're trying to achieve from the question. Do you wan't to bind the event so it occurs only once? Or simply unbind the event after it gets called at a particular point in your script? – hitautodestruct Commented Nov 5, 2012 at 10:36
Add a comment  | 

4 Answers 4

Reset to default 8

You can use the unbind() method:

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

You could use one instead of bind:

("#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.

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, e.g.

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

HTH Alex

发布评论

评论列表(0)

  1. 暂无评论