简体   繁体   English

打开时更改Bootstrap模态背景设置

[英]Change Bootstrap modal backdrop settings while open

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. 我在点击模态内的按钮后设置了$('#myModal').modal({ backdrop: 'static',keyboard:false }) ,但没有运气。

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) 我想要的效果类似于nakupanda的bootstrap-dialog的效果(参见Manipulating Buttons部分,当dialog.setClosable(true);在点击'Click to disable and spin'按钮时触发,模态不再可关闭)

Please see this jsfiddle . 请看这个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. 我知道这有可能以某种方式,但我无法分析nakupanda的代码。

Try changing modal 's option as below: 尝试更改modal的选项如下:

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

Here's the working example 这是工作示例

For anyone attempting to do this on v4, you need to change: 对于试图在v4上执行此操作的任何人,您需要更改:

$("#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. 只需定义一个文件就绪fn并将下面的代码放入其中。

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

You could try something like this: http://codepen.io/bwobst/pen/GrKBKy?editors=1010 你可以试试这样的东西: http//codepen.io/bwobst/pen/GrKBKy?edit = 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/ https://jsfiddle.net/25zbf094/2/

JavaScript 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. 我保持HTML基本不变,但在关闭按钮中添加了“关闭”的ID。 And as you can see above, you can still close the modal by pressing the X in the top right. 正如您在上面所看到的,您仍然可以通过按右上角的X来关闭模态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM