单个页面中的多个Bootstrap模态 [英] Multiple Bootstrap modals in a single page
问题描述
在一个页面中,大约有150个子菜单(可扩展).每次单击子菜单都会打开一个模态,其中包含该子菜单的特定信息.页面加载时首先获取数据.我想知道有什么更好的方法来实现模式.
In a page, there are around 150 submenus (expandable). Every click on a submenu opens a modal with specific information for the submenu. Data is fetched initially when the page loads. I wonder what is a better way to implement the modals.
我应该在同一页面中编写150个模态,还是编写一个模态然后动态创建内容?
Should I write 150 modals in the same page, or write one modal then create the content dynamically?
对于后一种选择,我需要一个示例.
For the latter option I need an example.
使用的技术:引导程序,HTML,CSS,jQuery.
Technologies used: Bootstrap, HTML, CSS, jQuery.
推荐答案
您可能会喜欢我创建的这个示例,如果您不了解任何地方,请告诉我.
You might like this example i have created, Let me know if you dont understand any where.
$(document).ready(function(e) {
// Initializing our modal.
$('#myModal').modal({
backdrop: 'static',
keyboard: false,
show: false,
});
$(document).on("click", ".modalButton", function() {
var ClickedButton = $(this).data("name");
// You can make an ajax call here if you want.
// Get the data and append it to a modal body.
$(".modal-body").html("<p>" + ClickedButton + "</p> <p>Some text in the modal.</p> ");
$('#myModal').modal('show');
});
});
<!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.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/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 modalButton" data-name="Clicked Modal 1" data-toggle="modal">Open Modal 1</button>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg modalButton" data-name="Clicked Modal 2" data-toggle="modal">Open Modal 2</button>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg modalButton" data-name="Clicked Modal 3" data-toggle="modal" >Open Modal 3</button>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg modalButton" data-name="Clicked Modal 4" data-toggle="modal">Open Modal 4</button>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg modalButton" data-name="Clicked Modal 5" data-toggle="modal">Open Modal 5</button>
<!-- Modal -->
<div class="modal fade" id="myModal" 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>
</div>
您可以只创建一个模态. 如前所述,您将拥有150个菜单,因此可以给他们一个通用的类名,并使用jquery来获取这些按钮/菜单的click事件. 如果需要,可以进行一些ajax调用以获取一些动态数据. 将您的回答附加到模态主体,然后仅显示模态.就是这样!
You can just create a single modal. and as you have mentioned you will have 150 menu so you can give them a common class name and using a jquery get the click event of those button/menu. You can have some ajax call to get some dynamic data if you want. append your response to the modal body and just show the modal. Thats it!!
这篇关于单个页面中的多个Bootstrap模态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!