问题描述
我想有条件地关闭引导程序模式框.如果我用$('#modal').modal('hide');
这,我的代码出了点问题.而且,如果我在HTML模板中使用data-dismiss="modal"
,则在单击按钮之前应该执行模态关闭操作,然后再执行我的实际功能.
I want to close bootstrap modal box conditionally. If I use$('#modal').modal('hide');
this, some thing goes wrong with my code. And If I use data-dismiss="modal"
in the HTML template, modal dismiss action performs before my actual functionality should be perform on button click.
那么,还有什么其他方法可以关闭引导程序模式,或者有什么想法可以在运行时使用data-dismiss="modal"
?
So, there is any other way to close bootstrap modal or any idea to use data-dismiss="modal"
at run time?
推荐答案
您可以使用本身使用data-dismiss
属性的自动模式关闭行为,也可以使用手动模式打开(例如,我猜您当前正在执行),订阅hide
事件并在事件上使用preventDefault
.
You can do it with auto modal closing behavior which uses the data-dismiss
attribute itself or with the manual modal opening (as i guess you are doing currently) , by subscribing to hide
event and use preventDefault
on the event.
$('yourmodalselector').on('hide',function(e){
if(yourConditionNotToCloseMet){
e.preventDefault();
}
});
演示
Demo2
请参见文档
Demo
Demo2
See Documentation
隐藏事件事件是在调用hide实例方法后立即触发的,该方法将以枯燥的方式被调用,这是防止模式关闭的最佳位置.
hide event event is fired immediately when the hide instance method has been called, which gets called wither way and this is the best place to prevent the modal from closing.
这篇关于在不使用“隐藏"的情况下关闭引导程序模态和“数据删除"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!