[英]Twitter Bootstrap Modal change Content via Ajax
我知道,这里有很多关于同一主题的问题。 但是我找不到我的问题的解决方案。 所以我有一个布局,并且在body标签关闭之前,有一个模态窗口:
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
CONTENT
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</body>
我的网站上有一个链接可以切换模式窗口:
<a data-toggle="modal" class="btn btn-info" href="myRemote.php?id=1" data-target="#myModal">Click</a>
我的远程php仅包含“ <php echo $_GET['id'];?>
”进行测试。 将来会有来自数据库的内容。
因此,每次我单击模态链接时,内容都会更改。 这不是问题,我知道如何在php等中执行此操作。
我写了JS,应该更改模式内容:
$(document).ready(function() {
// Fill modal with content from link href
$("#myModal").on("show.bs.modal", function(e) {
var link = $(e.relatedTarget);
$(this).find(".modal-body").load(link.attr("href"));
});
})
问题:
当我单击链接时,将打开模式窗口。 完善。 但是,除了模式窗口消失之外,在我网站的顶部位置还有“ 1”。 但这不是很好,因为我只希望模态主体中的内容“ CONTENT”更改为“ 1”。 我在这里做错了什么?
还有-另一个问题。 如果我每次使用$ _GET ['id']更改内容时都不会消失1,但是我想在此处更改内容动态,因此在此示例中,模态主体类中应有一个“ 2”模式窗口,如果我单击这样的链接:
<a data-toggle="modal" class="btn btn-info" href="myRemote.php?id=2" data-target="#myModal">Click</a>
有人可以帮我吗?
制作如下链接:
<a class="btn btn-info openModal" data-href="myRemote.php?id=1">Click</a>
<a class="btn btn-info openModal" data-href="myRemote.php?id=1">Click 2</a>
在Js上:
$(document).ready(function() {
// Fill modal with content from link href
$(".openModal").on("click", function(e) {
var link = $(this).data("href");
$('#myModal').modal("show");
$('#myModal .modal-body').load(link );
});
})
你可以试试看吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.