开发者

Jquery unbind events bound with one()

开发者 https://www.devze.com 2023-03-30 15:06 出处:网络
Is there any method to unbind an event that has been bound with on开发者_Python百科e()? Sort of like unone()Quote from JQuery.com:

Is there any method to unbind an event that has been bound with on开发者_Python百科e()? Sort of like unone()


Quote from JQuery.com:

You cannot unbind a listener created using .one(). If you want to be able to unbind something that has to occur only once but still be able to unbind it before it occurs, you have to use .bind()

Something like:

$("#element").on("click",function(event){
    //do stuff here
    $(this).off(event);
}


According to a comment on the doc page:

Note: you cannot unbind a listener created using .one(). If you want to be able to unbind something that has to occur only once but still be able to unbind it before it occurs, use the example provided for unbinding an event after it's called, and bind it using .bind().

http://api.jquery.com/one/


You unbind whatever event that is bounded with .one().

$('your_element').unbind('event_called_with_one');

$('input[type=text]').focus(function(){
    $(this).one('keypress',function(){console.info('This should not print!');});
    if($('input[type=checkbox]').prop('checked'))$(this).unbind('keypress');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type=text>
<label><input type=checkbox checked>unbind</label>

0

精彩评论

暂无评论...
验证码 换一张
取 消