最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change Bootstrap modal backdrop settings while open - Stack Overflow

programmeradmin3浏览0评论

I'm trying to change the backdrop setting of my modal to 'static' as a response to an event, so that it becomes non-dismissable. I tried this by setting $('#myModal').modal({ backdrop: 'static',keyboard:false }) after clicking on a button inside the modal, with no luck.

The effect I want is similar to the effect from nakupanda's bootstrap-dialog (see Manipulating Buttons section, when dialog.setClosable(true); is triggered on click of the 'Click to disable and spin' button, the modal is no longer closable)

Please see this jsfiddle.

I know this question has already been asked here but it doesn't have a proper answer. I know this is possible somehow, but I fail to analyze nakupanda's code.

I'm trying to change the backdrop setting of my modal to 'static' as a response to an event, so that it becomes non-dismissable. I tried this by setting $('#myModal').modal({ backdrop: 'static',keyboard:false }) after clicking on a button inside the modal, with no luck.

The effect I want is similar to the effect from nakupanda's bootstrap-dialog (see Manipulating Buttons section, when dialog.setClosable(true); is triggered on click of the 'Click to disable and spin' button, the modal is no longer closable)

Please see this jsfiddle.

I know this question has already been asked here but it doesn't have a proper answer. I know this is possible somehow, but I fail to analyze nakupanda's code.

Share Improve this question edited May 23, 2017 at 12:08 CommunityBot 11 silver badge asked Jan 2, 2017 at 3:37 James AJames A 4875 silver badges14 bronze badges 1
  • Possible duplicate of Change backdrop to static for open bootstrap modal – Exik Commented Mar 7, 2018 at 15:06
Add a comment  | 

5 Answers 5

Reset to default 11

Try changing modal's option as below:

$('#myModal').data('bs.modal').options.backdrop = 'static';

Here's the working example

For anyone attempting to do this on v4, you need to change:

$("#myModal").data('bs.modal').options.backdrop = 'static';

to

$("#myModal").data('bs.modal')._config.backdrop = 'static';

why don't you just initialize the modal with options at page load instead of on button click?

just define a document ready fn and put the below code in it.

$('#myModal').modal({
    backdrop: 'static',
    keyboard: false
  })

You could try something like this: http://codepen.io/bwobst/pen/GrKBKy?editors=1010

$('#open-modal').click(function(evt) {
  $('#myModal').modal({backdrop: 'static'});
  $('.modal button').prop('disabled', true);
});

When you click the 'open modal' button it sets the backdrop to static and disables the buttons within the modal.

I came up with a solution. The issue is that even if you set the backdrop to static, there is still a click listener.

My solution is here. https://jsfiddle.net/25zbf094/2/

JavaScript

$('#makeIndismissable').click(function(){
  $('#myModal').prop('onclick',null).off('click');
});

$('#close').click(function(){
    $('#myModal').modal("hide")
})

I kept the HTML mostly unchanged, but added the id of "close" to the close button. And as you can see above, you can still close the modal by pressing the X in the top right.

发布评论

评论列表(0)

  1. 暂无评论