Change backdrop to static for open bootstrap modal

后端 未结 5 2074
感情败类
感情败类 2020-12-19 05:33

Can I change backdrop to \'static\' while my modal is open?

I have modal with form submit button. When I click this button I show loading spinner on my modal and th

5条回答
  •  春和景丽
    2020-12-19 06:03

    The simplest method I've come up with is attaching to the hide.bs.modal event and calling preventDefault(). This doesn't technically set the backdrop to static, but it achieves the same effect in a toggleable manner.

    let $myModal = $('#myModal');
    
    function preventHide(e) {
        e.preventDefault();
    }
    
    // if you have buttons that are allowed to close the modal
    $myModal.find('[data-dismiss="modal"]').click(() => 
        $myModal.off('hide.bs.modal', preventHide));
    
    function disableModalHide() {
        $myModal.on('hide.bs.modal', preventHide);
    }
    
    function enableModalHide() {
        $myModal.off('hide.bs.modal', preventHide);
    }
    

    (Disclaimer, I didn't test making buttons allowed to hide the modal, as that wasn't my scenario. If it doesn't work, just call .modal('hide') from the click() callback.)

提交回复
热议问题