![](/img/trans.png)
[英]How to open two modal dialogs in Twitter Bootstrap at the same time
[英]Bootstrap Modal - same modal multiple open dialogs
我有一个模态代码和模态开启器链接。
当我单击链接模态打开时,后面的 JavaScript 会发出 Ajax 请求并填充模态内的元素值。
这很好用。
但是我需要在模态对话框中生成模态开启器链接,它会再次打开相同的模态。
我想打开另一个窗口,以便这个新窗口与第一个窗口重叠。 因此,两个(或更多)打开相同模式的弹出窗口。
首先,当我在模态窗口中生成 modal-opener 链接时,链接已失效。
比我从 modal-opener 链接中删除了data-toggle="modal"
并在单击链接时将此 jQuery 代码用于侦听和打开模态:
$(".modal_order_details_opener").click(function () {
$('#modal_order_details').modal('show');
});
这有效,但不是我想要的方式。
它打开原始模态并且链接在那里,当我单击该链接打开另一个窗口浏览器打开另一个模态对话框但原始模态对话框消失。
所以问题是:我可以打开两个或多个相同模态的窗口吗?
一个模态代码,多个打开的对话框实例。
我看过的所有示例都打开了两个不同的模态。
我问的是相同的模态和同时打开的更多对话框。 基本上从模态内打开模态。
同模态。
谢谢。
我已经找到了解决方案,所以我会回答我自己的问题,以防万一有人想知道。
整个事情的关键是 jQuery clone()
函数。
基本上你使用两个 DOM 对象:
1 - 打开模态的链接 - 具有名为modal_details_opener
“类”
2 - 模态 HTML 本身 - 主<div>
具有名为modal_details
“id”
首先你需要 JavaScript 回调函数,我称之为callback()
。
因此,在 .js 文件末尾或文档准备就绪时的全局范围内,单击打开模态的链接时注册callback()
:
$('.modal_details_opener').click(callback);
请记住,模态体内部有链接,可以打开相同的模态。
所以在callback()
主体中,我们必须:
1 - 在文档上找到模态
2 - 克隆它
3 - 其主体内的模态具有再次打开相同模态的链接,找到这些链接并在其“单击”事件上递归绑定callback()
函数
function callback() {
// Find modal body on the document
var $currentDetailsModal = $('#modal_details');
// Clone modal and save copy
var $cloneDetailsModal = $currentDetailsModal.clone();
// Find links in this modal which open this modal again and bind this function to their click events
$(".modal_details_opener", $cloneDetailsModal).click(callback);
}
请注意,您必须将克隆的模态作为上下文传递给$()
函数,以仅隔离到每个打开的模态克隆中的链接。
以下代码将回答您的问题。 如果它不起作用,请让我知道我会以另一种方式思考。
<!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="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Large Modal</h2>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Large Modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog modal-lg">
<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>This is a large modal.</p>
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal2">ClickForAnotherModel</button>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="myModal2" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Another Modal</h4>
</div>
<div class="modal-body">
<p>This is Another Modal on Modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.