Change Bootstrap modal option once it already exists(一旦已经存在就更改 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
这篇关于一旦已经存在就更改 Bootstrap 模式选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:一旦已经存在就更改 Bootstrap 模式选项
基础教程推荐
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 动态更新多个选择框 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01