Bind a function to Twitter Bootstrap Modal Close

2019-01-01 02:31发布

I am using the Twitter Bootstrap lib on a new project and I want for part of the page to refresh and retrieve the latest json data on modal close. I dont see this anywhere in the documentation can someone point it out to me or suggest a solution.

Two problems with using the documented methods

 $('#my-modal').bind('hide', function () {
   // do something ...
 });

I attach a "hide" class to the modal already so it does not display on page load so that would load twice

even if I remove the hide class and set the element id to display:none and add console.log("THE MODAL CLOSED"); to the function above when I hit close nothing happens.

11条回答
十年一品温如言
2楼-- · 2019-01-01 03:11

Bootstrap 4

$('#my-modal').on('hidden.bs.modal', function () {
  window.alert('hidden event fired!');
});

See this JSFiddle for a working example:

http://jsfiddle.net/aq9Laaew/120440/

See the Modal Events section of the docs here:

https://getbootstrap.com/docs/4.1/components/modal/#events

查看更多
闭嘴吧你
3楼-- · 2019-01-01 03:13

I've seen many answers regarding the bootstrap events such as hide.bs.modal which triggers when the modal closes.

There's a problem with those events: any popups in the modal (popovers, tooltips, etc) will trigger that event.

There is another way to catch the event when a modal closes.

$(document).on('hidden','#modal:not(.in)', function(){} );

Bootstrap uses the in class when the modal is open. It is very important to use the hidden event since the class in is still defined when the event hideis triggered.

This solution will not work in IE8 since IE8 does not support the Jquery :not() selector.

查看更多
泪湿衣
4楼-- · 2019-01-01 03:13

I'm jumping in super late here, but for people using Bootstrap modals with React I've been using MutationObserver to detect changes in a modal's visibility and adjusting the state accordingly - this method could be applied to run any function when the modal is closed:

//react stuff
componentDidMount: function() {
    var self = this;
    $(function() {
        $("#example_modal").addClass('modal');
        //use MutationObserver to detect visibility change
        //reset state if closed
        if (MutationObserver) {
        var observer = new MutationObserver(function(mutations) {
            //use jQuery to detect if element is visible
            if (!$('#example_modal').is(':visible')) {
                //reset your state
                self.setState({
                    thingone: '',
                    thingtwo: ''
                });
            }
        });
        var target = document.querySelector('#example_modal');
            observer.observe(target, {
                attributes: true
          });
        }
    });
}

For those wondering about modern browser support, CanIUse has MutationObserver's coverage at around 87%

Hope that helps someone :)

Cheers,

Jake

查看更多
妖精总统
5楼-- · 2019-01-01 03:20

I was having the same issues as some with

$('#myModal').on('hidden.bs.modal', function () {
// do something… })

You need to place this at the bottom of the page, placing it at the top never fires the event.

查看更多
不再属于我。
6楼-- · 2019-01-01 03:24

I would do it like this:

$('body').on('hidden.bs.modal', '#myModal', function(){ //this call your method });

The rest has already been written by others. I also recommend reading the documentation:jquery - on method

查看更多
梦醉为红颜
7楼-- · 2019-01-01 03:26

Bootstrap 3 & 4

$('#myModal').on('hidden.bs.modal', function () {
    // do something…
})

Bootstrap 3: getbootstrap.com/javascript/#modals-events

Bootstrap 4: getbootstrap.com/docs/4.1/components/modal/#events

Bootstrap 2.3.2

$('#myModal').on('hidden', function () {
    // do something…
})

See getbootstrap.com/2.3.2/javascript.html#modals → Events

查看更多
登录 后发表回答