jQuery 不使用“隐藏”和“数据关闭”关闭引导模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16972809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Close bootstrap modal without using "hide" and "data-dismiss"
提问by Ruchi Agarwal
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.
我想有条件地关闭引导模式框。如果我使用
$('#modal').modal('hide');
它,我的代码就会出现问题。如果我data-dismiss="modal"
在 HTML 模板中使用,模态解除操作会在我的实际功能应该在按钮单击时执行之前执行。
So, there is any other way to close bootstrap modal or any idea to use data-dismiss="modal"
at run time?
那么,还有其他方法可以关闭引导模式或data-dismiss="modal"
在运行时使用的任何想法吗?
采纳答案by PSL
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.
您可以通过使用data-dismiss
属性本身的自动模式关闭行为或手动模式打开(正如我猜你目前正在做的那样)来完成,通过订阅hide
事件并preventDefault
在事件上使用。
$('yourmodalselector').on('hide',function(e){
if(yourConditionNotToCloseMet){
e.preventDefault();
}
});
Demo
演示
Demo2
演示2
See Documentation
查看文档
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.
hide 事件事件在调用 hide 实例方法时立即触发,该方法以枯萎方式调用,这是防止模态关闭的最佳位置。
回答by Bugfixer
Make another button like this
制作另一个这样的按钮
<button type="button" class="btn btn-warning btn-lg shiny" data-dismiss="modal" aria-hidden="true">Cancel</button>
This button contains data-dismiss="modal"
.You can hide this if you want.
此按钮包含data-dismiss="modal"
。您可以根据需要隐藏它。
Now You can use any other function in a customized way and when you want to hide the modal you can call
现在您可以以自定义方式使用任何其他功能,当您想隐藏模式时,您可以调用
$(".btn-warning").click();