【发布时间】:2023-03-21 22:00:02
【问题描述】:
我的 Bootstrap 模式有问题。我正在使用$.ajax 调用,因此我可以将 POST 数据发送到单独的 .php 文件,以显示在打开的模式中。我一直在使用 Bootstrap 3,我没有任何错误,但是当我升级到 Bootstrap 4 时,我的模态不会关闭。
最初我在 StackOverflow 上找到了使用 show.bs.modal 的技术,因为我进行了研究以构建我的网站。为了测试目的,我尽可能简单地剥离了我的代码,所以我可以在这个问题中复制它。在下面的代码中,标题为“Launch demo modal”的按钮将打开和关闭,但“Launch ajax demo modal”只会打开(我必须刷新才能让它消失)。
我非常努力地遵循 Bootstrap 关于如何安排链接和对其库的引用的建议。请注意show.bs.modal“on”函数中涉及一个名为test-modal.php的文件。
请在下面查看我正在尝试的精简测试代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
<link rel="icon" href="http://si.hclibrary.org/wp-content/uploads/2017/02/Icon_SmallSi-01.png" sizes="32x32">
<title>HCLS IT Helpdesk Request Site</title>
</head>
<body>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Launch demo modal
</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal2">
Launch ajax demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<div class="modal fade" id="exampleModal2" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"></div>
<script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js" integrity="sha384-a5N7Y/aK3qNeh15eJKGWxsqtnX/wWdSZSKp+81YjTmS15nvnvxKHuzaWwXHDli+4" crossorigin="anonymous"></script>
<script>
$( document ).ready(function() {
$('#exampleModal2').on('show.bs.modal', function(e) {
$.ajax({
type:'post',
url:'test-modal.php',
data:{
id:"id",
type:"type"
},
success:function(data) {
$('#exampleModal2').html(data);
}
});
});
});
</script>
</body>
</html>
而且我的 test-modal.php 文件非常简单:
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Ajax Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
有没有其他人遇到过模态无法正常关闭的问题? Bootstrap 4 中是否有一些已被弃用的东西,所以它不应该像以前那样工作?还是有另一种方法我应该将 POST 变量发送到打开的模式?我已经坚持了很长时间,所有的建议都非常感谢。如果我应该提供更多信息,请告诉我。
【问题讨论】:
-
模态元素被你的ajax调用替换......所以新的HTML元素不会被引导程序解析。为什么不只加载模态体?
-
@LouysPatriceBessette 是否可以加载带有传递给它的变量的模态体,比如通过 POST?
-
@LouysPatriceBessette 既然您提到它,这绝对是合乎逻辑的。我想知道为什么它以前可以与 Bootstrap 3 一起使用。
-
在 Ajax
success中,您只能使用$('#exampleModal2 .modal-body').html(data);加载正文 -
Bootstrap 是一个在加载时运行的 JS 脚本。脚本运行时不存在的动态元素总是同样的问题。阅读famous answer ;)
标签: javascript php jquery ajax twitter-bootstrap