情境:两个模态(modal1和modal2。模态1应该在模态1关闭后打开模态2。
javascript代码:
$("#btToOpenModal2").click(function() { $('#modal1').modal('hide'); setTimeout(function(){ $('#modal2').modal('show'); }, 1000); });
问题: 新模态只显示灰色背景。
如果我先用按钮打开modal2(然后关闭)然后我使用javascript代码(其他按钮),那么它的工作。
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#modal2">Open Modal</button>
我真的不明白为什么那不起作用,也许有人已经有了这个问题。
答案 0 :(得分:2)
做这样的事情..
$('#modal1').on('hidden.bs.modal', function (e) {
// do something...
});
在转换完成后关闭modal1时会触发hidden.bs.modal 或者你想在它关闭时立即开火,使用以下内容:
$('#modal1').on('hide.bs.modal', function (e) {
// do something...
});
答案 1 :(得分:1)
我创建了一个简单的例子而没有别的东西。现在Modal1(id) - &gt; Button-&gt; open2(id)不再打开Modal2
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Modal Example</h2>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal1">Open Modal1</button>
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal2">Open Modal2</button>
<!-- Modal2 -->
<div class="modal fade" id="myModal2" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- Modal1 -->
<div class="modal fade" id="myModal1" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<button type="button" class="btn btn-info btn-lg" id="open2">Open Modal</button>
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$("#open2").click(function() {
$('#myModal1').modal('hide');
setTimeout(function() {
$('#mymodal2').modal('show');
}, 1000);
});
</script>
</body>
</html>