一旦已经存在就更改 Bootstrap 模式选项

我正在使用 引导模式.我宣布它,我称之为它,我展示它......一切似乎都很好.

但是,如果我有一个已经存在的模式,之前显示的键盘"属性为 false,我想在旅途中更改它怎么办?我的意思是:

首先,我创建了一个 Modal 来执行此操作(如您所见,我声明了将键盘属性设置为 false 的模态):

$('#myModal').modal({显示:假,背景:真实,键盘:假});

然后我声明了这个事件处理程序,这意味着如果发生某事",我希望将键盘属性设置为 true.

 $('#myModal').on('shown', function(e) {如果(某事){$('#myModal').modal({keyboard: true});}}

所以,我去的时候

$("#myModal").show();

事件处理程序没有做它应该做的事情,因为一旦按下 Escape 键我就没有关闭模式.但我完全确定某事"是真的,并且我已经检查并重新检查了 $('#myModal').modal({keyboard: true}) 是否已执行.

关于为什么这不更新配置选项的值的任何线索?

解决方案

要更改已经启动的 Bootstrap 插件的配置设置,例如 Modal,您需要访问附加到元素的插件对象,例如 $('#pluginElement').data['somePlugin'] 然后在里面设置options.

对于模态,您需要:

$('#myModal').data('modal').options.keyboard = true;

JSFiddle 演示(旧)

<小时><块引用>

对于 Bootstrap 3(如 @Gerald 的评论中所述),您需要 bs.modal:

$('#myModal').data('bs.modal').options.keyboard = true;

等待模式示例

I'm using Bootstrap Modal. I declare it, I call it, I show it...everything seems to be ok.

But what if I have an already existing modal previously shown with "keyboard" property to false and I want to change it on the go? I mean something like:

First, I create a Modal doing this (as you can see, I declare the modal putting keyboard property to false):

$('#myModal').modal({
    show: false,
    backdrop: true,
    keyboard: false
});

But then I declare this event handler, that means that if "something" happens, I want the keyboard property to be set to true.

 $('#myModal').on('shown', function(e) {
    if (something){
        $('#myModal').modal({keyboard: true});
    }
 }

So, when I go

$("#myModal").show();

The event handler is not doing what it is supposed to, as I am not getting to close the modal once Escape key is pressed. But I am completely sure that "something" is true and I have checked and re-checked that $('#myModal').modal({keyboard: true}) is executed.

Any clue as to why this isn't updating the value of configuration option?

解决方案

To change configuration settings on already initiated Bootstrap plugin, such as the Modal, you need to access the plugin object attached to the element, like $('#pluginElement').data['somePlugin'] and then set the options in it.

For the Modal, you need:

$('#myModal').data('modal').options.keyboard = true;

JSFiddle Demo (old)


For Bootstrap 3 (as mentioned in comments by @Gerald ), you need bs.modal:

$('#myModal').data('bs.modal').options.keyboard = true;

Waiting Modal Example

相关文章