一旦它存在,就更改Bootstrap模态选项 [英] Change Bootstrap modal option once it already exists

查看:149
本文介绍了一旦它存在,就更改Bootstrap模态选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Bootstrap Modal 。我宣布它,我称之为,我展示它......一切似乎都没问题。

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:

首先,我创建一个Modal这样做(你可以看到,我声明模态把键盘属性设置为false):

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
});

然后我宣布这个事件处理程序,这意味着如果某事发生,我想要键盘属性设置为true。

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});
    }
 }

所以,当我去的时候

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

事件处理程序没有做到它应该做的事情因为按下Escape键后我不会关闭模态。但我完全确定某事是真的,我已检查并重新检查 $('#myModal')。执行模态({keyboard:true})

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?

推荐答案

要更改已启动的Bootstrap插件(例如Modal)的配置设置,您需要ess附加到元素的插件对象,如 $('#pluginElement')。data ['somePlugin'] 然后设置选项 in。

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.

对于模态,你需要:

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



JSFiddle Demo(旧)






JSFiddle Demo (old)



对于Bootstrap 3(如在@Gerald的评论中提到,你需要 bs.modal



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

这篇关于一旦它存在,就更改Bootstrap模态选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆