How do I know which button is clicked when the bootstrap modal closes?

后端 未结 5 1556
逝去的感伤
逝去的感伤 2020-12-23 13:42

Here is my modal html code:

5条回答
  •  悲&欢浪女
    2020-12-23 14:42

    To extend @JoshCrozier's answer:

    It would be nice if they had a property like that associated with the hide.bs.modal/hidden.bs.modal events. As of writing this, there currently isn't


    This will emulate a similar behaviour, that attaches the clicked button as relatedTarget for later listeners:

    $( '.modal-footer .btn[data-dismiss="modal"]' ).on( 'click', function() {
        var target = this
    
        $( target ).closest( '.modal' )
            .one( 'hide.bs.modal hidden.bs.modal', function( event ) {
                event.relatedTarget = target
            } )
    } )
    

    The selector and listener can be further optimized depending on how the modals are used in a project. For example: if you know you're not going to use hide.bs.modal you can just modify hidden.bs.modal's event instead.

提交回复
热议问题