Prevent Bootstrap Modal from disappearing when cli

2020-01-25 02:54发布

I'm using the Twitter Bootstrap modal as a wizard window, and would like to prevent the user from closing it when clicking outside of the modal or when pressing escape. Instead, I want it to be closed when the user presses the finish button. How could I achieve this scenario?

16条回答
我命由我不由天
2楼-- · 2020-01-25 03:50
jQuery('#modal_ajax').modal('show', {backdrop: 'static', keyboard: false});
查看更多
我只想做你的唯一
3楼-- · 2020-01-25 03:51

If you need to setup this after the modal is shown, you can use @Nabid solution. However, sometimes you still need to allow some method to close the modal. Assuming you have a button with class really-close-the-modal, which should really close the modal, you can use this code (jquery):

var closeButtonClicked = false;

$('.really-close-the-modal').on('click', function () {
    closeButtonClicked = true;
});

$('#myModal').on('hide.bs.modal', function (e) {
    if (!closeButtonClicked) {
        e.preventDefault();
        e.stopPropagation();
        return false;
    }
    closeButtonClicked = false;
});

This isn't really nice code design, but it helped me in a situation where the modal was shown with a loader animation, until an ajax request returned, and only then could I know if the modal needed to be configured to prevent "implicit" closing. You can use a similar design to prevent closing the modal while it's still loading.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2020-01-25 03:53

I use these attributes and it works, data-keyboard="false" data-backdrop="static"

查看更多
别忘想泡老子
5楼-- · 2020-01-25 03:54

If you need disable clicking outside but enable pressing escape

$('#myModal').modal({
    backdrop: 'static',   // This disable for click outside event
    keyboard: true        // This for keyboard event
})
查看更多
登录 后发表回答