jQuery删除.change()处理程序

JRu*_*lle 2 html forms jquery

我一直在试图找出如何删除.change()jquery处理程序,但没有运气。当选中单选按钮时,我正在使用它来激活某些表单字段,但是我不希望它为以后的每次更改触发。

$('.radio').change(function(){
    alert("changed");
    $('.fields').removeAttr('disabled');
    //this is where I try removing the change handler
    $(this).off('change');
});
Run Code Online (Sandbox Code Playgroud)


JSfiddle演示

Sim*_*gro 6

更改

// 'this' only removing the handler from the radio button that was changed
$(this).off('change');
Run Code Online (Sandbox Code Playgroud)

// removing the handler of all radio button
$('.radio').off('change');
Run Code Online (Sandbox Code Playgroud)

您的代码

$('.radio').change(function(){
  alert("changed");
  $('.fields').removeAttr('disabled');
  $('.radio').off('change');
});
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/q5jqqgnt/3/

  • 我的原始代码只是从已更改的单选按钮中删除处理程序,而不是从组中删除。谢谢。 (2认同)