問題描述
我正在使用 引導模式.我宣布它,我稱之為它,我展示它......一切似乎都很好.
但是,如果我有一個已經存在的模式,之前顯示的鍵盤"屬性為 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 模式選項的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!