我知道,这里有很多相同主题的问题。 但我找不到解决我的问题的方法。 所以我有一个布局,在 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"));
});
})
the problem:
当我单击链接时,模式窗口将打开。完美的。 但模态窗口消失了,在我网站的顶部位置有“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 );
});
})
你能尝试一下吗?
关于javascript - Twitter Bootstrap 模态通过 Ajax 更改内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29161862/