如何从 javascript 函数中显示引导模式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11839472/
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
How do I show a bootstrap modal from within a javascript function?
提问by silvster27
I have a button on my page that when it is pushed it triggers some javascript functions to occur, and one of those javascript functions to open a bootstrap modal, but I can't seem to get it to work. Here is my code... please help.
我的页面上有一个按钮,当它被按下时,它会触发一些 javascript 函数发生,其中一个 javascript 函数打开引导模式,但我似乎无法让它工作。这是我的代码...请帮忙。
//elsewhere on the form is the button that triggers the javascript
<a href="#" class="btn btn-danger" id="cancelPObtn" data-dismiss="modal">Yes, cancel it</a>
<!-- CANCEL A PO MODAL -->
<div class="modal hide fade" id="error-dialog" style="display: none;">
<div class="modal-header">
<a class="close" data-dismiss="modal">x</a>
<h3>Cancel Purchase Order?</h3>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<a href="#" class="btn btn-danger btn-modal btn-cancel" data-dismiss="modal">Yes, cancel it</a>
<a href="#" class="btn" data-dismiss="modal">Nevermind</a>
</div>
</div>
$('.btn-danger').click(function(event) {
if(some random conditional statement){
//some stuff happens here
}
else{
//Show form validation error modal-- I know this part is triggered but the modal will not show
$("#error-dialog").modal("show");
}
});
采纳答案by Franklin Clark
you're missing if () {
part of the conditional statement
你缺少if () {
条件语句的一部分
回答by Andy
$("#error-dialog").modal("show");
$("#error-dialog").modal("show");
This is the correct way, your code simply has an error. Go in to Chrome Console and run this command, it'll work.
这是正确的方法,您的代码只是有错误。进入 Chrome 控制台并运行此命令,它会起作用。
回答by JAR.JAR.beans
It's a bit hard to see what you want to do with this partial code, but here is a working code based on yours:
很难看出你想用这个部分代码做什么,但这里有一个基于你的工作代码: