Bootstrap: Open Another Modal in Modal

后端 未结 21 940
难免孤独
难免孤独 2020-11-27 11:28

So, I\'m using this code to open another modal window in a current opened modal window:



        
21条回答
  •  感情败类
    2020-11-27 11:54

    Working on a project that has a lot of modals calling other modals and a few HTML guys that might not know to initiate it everytime for each button. Came to a similar conclusion as @gmaggio, begrudgingly after going the long way around first.

    EDIT: Now supports modals called via javascript.

    EDIT: Opening a scrolling modal from another modal now works.

    $(document).on('show.bs.modal', function (event) {
        if (!event.relatedTarget) {
            $('.modal').not(event.target).modal('hide');
        };
        if ($(event.relatedTarget).parents('.modal').length > 0) {
            $(event.relatedTarget).parents('.modal').modal('hide');
        };
    });
    
    $(document).on('shown.bs.modal', function (event) {
        if ($('body').hasClass('modal-open') == false) {
            $('body').addClass('modal-open');
        };
    });
    

    Just place the modal calling button in as normal, and if it is picked up to be inside a modal, it will close the current one first before opening the one specified in data-target. Note that relatedTarget is provided by Bootstrap.

    I also added the following to smooth out the fading a bit: I am sure more can be done though.

    .modal-backdrop.fade + .modal-backdrop.fade {
        transition: opacity 0.40s linear 0s;
    }
    

提交回复
热议问题