In Twitter Bootstrap, how do I unbind an event from the closing of a modal dialog?

前端 未结 2 948
轻奢々
轻奢々 2021-02-07 03:22

I have a function that is bound to the action of hiding a modal dialog.

I\'m using code similar to the accepted answer to this question.

$(\'#myModal\')         


        
相关标签:
2条回答
  • 2021-02-07 03:33

    Use:

    $('#myModal').off('hidden');
    

    Why not unbind?

    As of jQuery 3.0, .unbind() has been deprecated. It was superseded by the .off() method since jQuery 1.7, so its use was already discouraged.

    Source: jQuery API.

    0 讨论(0)
  • 2021-02-07 03:47

    You can do something like to unbind all events tied to the modal element:

    Unbind all events in the modal:

    /* First option */
    $('#myModal').on('hidden', function (e) {
        $(e.currentTarget).unbind(); // or $(this)        
    });
    
    /* Second option is to call it directly when needed */
    $('#myModal').unbind();
    

    The Bootrap modal also have specific events tied to it, so you can also specify which event(s) you want to unbind.

    /* Events are 'hidden', 'hide', 'show', 'shown' */
    $('#myModal').unbind(/* specific event here */);
    

    If you wish to remove events tied to the content of the modal, you can simply just empty the elements within $('#myModal').empty() and unbind those elements appropriately.

    0 讨论(0)
提交回复
热议问题