Bootstrap modal: close current, open new

前端 未结 27 1668
离开以前
离开以前 2020-11-29 00:23

I have looked for a while, but I can\'t find a solution for this. I want the following:

  • Open an URL inside a Bootstrap modal. I have this working off course. S
相关标签:
27条回答
  • 2020-11-29 01:07

    This code, close modal opened, open the new modal but inmediatly, new modal is closed.

    $(nameClose).modal('hide');
    $(nameOpen).modal('show');
    

    My solution for open new modal after close other is:

    function swapModals(nameClose,nameOpen) {
        $(nameClose).modal('hide');
        $(nameClose).on('hidden.bs.modal', function () {
            console.log('Fired when hide event has finished!');
            $(nameOpen).modal('show');
        });
    }
    
    0 讨论(0)
  • 2020-11-29 01:08

    Try this

        $('#model1').modal('hide');
    setTimeout(function(){
        $('#modal2').modal('show');
    },400);
    
    0 讨论(0)
  • 2020-11-29 01:08

    None of the answers helped me since I wanted to achieve something which was exactly the same as mentioned in the question.

    I have created a jQuery plugin for this purpose.

    /*
     * Raj: This file is responsible to display the modals in a stacked fashion. Example:
     * 1. User displays modal A
     * 2. User now wants to display modal B -> This will not work by default if a modal is already displayed
     * 3. User dismisses modal B
     * 4. Modal A should now be displayed automatically -> This does not happen all by itself 
     * 
     * Trying to solve problem for: http://stackoverflow.com/questions/18253972/bootstrap-modal-close-current-open-new
     * 
     */
    
    var StackedModalNamespace = StackedModalNamespace || (function() {
        var _modalObjectsStack = [];
        return {
            modalStack: function() {
                return _modalObjectsStack;
            },
            currentTop: function() {
                var topModal = null;
                if (StackedModalNamespace.modalStack().length) {
                    topModal = StackedModalNamespace.modalStack()[StackedModalNamespace.modalStack().length-1];
                }
                return topModal;
            }
        };
    }());
    
    // http://stackoverflow.com/a/13992290/260665 difference between $.fn.extend and $.extend
    jQuery.fn.extend({
        // https://api.jquery.com/jquery.fn.extend/
        showStackedModal: function() {
            var topModal = StackedModalNamespace.currentTop();
            StackedModalNamespace.modalStack().push(this);
            this.off('hidden.bs.modal').on('hidden.bs.modal', function(){   // Subscription to the hide event
                var currentTop = StackedModalNamespace.currentTop();
                if ($(this).is(currentTop)) {
                    // 4. Unwinding - If user has dismissed the top most modal we need to remove it form the stack and display the now new top modal (which happens in point 3 below)
                    StackedModalNamespace.modalStack().pop();
                }
                var newTop = StackedModalNamespace.currentTop();
                if (newTop) {
                    // 3. Display the new top modal (since the existing modal would have been hidden by point 2 now)
                    newTop.modal('show');
                }
            });
            if (topModal) {
                // 2. If some modal is displayed, lets hide it
                topModal.modal('hide');
            } else {
                // 1. If no modal is displayed, just display the modal
                this.modal('show');
            }
        },
    });
    

    Working Fiddle for reference, JSFiddle: https://jsfiddle.net/gumdal/67hzgp5c/

    You just have to invoke with my new API "showStackedModal()" instead of just "modal('show')". The hide part can still be the same as before and the stacked approach of showing & hiding the modals are automatically taken care.

    0 讨论(0)
  • 2020-11-29 01:10

    if you use mdb use this code

        var visible_modal = $('.modal.show').attr('id'); // modalID or undefined
        if (visible_modal) { // modal is active
            $('#' + visible_modal).modal('hide'); // close modal
        }
    
    0 讨论(0)
  • 2020-11-29 01:11

    I might be a bit late to this but I think I've found working solution.

    Required -

    jQuery

    All modals with a closing/dismissal button having attributes set as follows -

    <button type="button" class="btn close_modal" data-toggle="modal" data-target="#Modal">Close</button>  
    

    Please see the class close_modal added to the button's classes

    Now to close all existing modals, we'll call

    $(".close_modal").trigger("click");
    

    So, wherever you want to open a modal

    Just add the above code and your all open modals shall get closed.

    Then add your normal code to open the desired modal

    $("#DesiredModal").modal();
    
    0 讨论(0)
  • 2020-11-29 01:12

    If you want to close the previously opened modal while opening the new modal then you must do it from javascript/jquery by first closing the current open modal and then give a 400ms timeout to allow it to close and then open the new modal like below code :

    $('#currentModal').modal('hide');
    
    setTimeout(function() {
           //your code to be executed after 200 msecond 
           $('#newModal').modal({
               backdrop: 'static'//to disable click close
       })
    }, 400);//delay in miliseconds##1000=1second
    

    If you try to do it with the data-dismiss="modal" then it will have the scroll issue as mentioned by @gravity and @kuldeep in comments.

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