我的代码以这种方式工作正常
posts.php?id=<?php echo $post_id?; ?>
它会转到链接并显示帖子。
我需要停止进入该链接并在模式中显示它。我该怎么做
注意:我可以简单地将其包含在模态中,但不能传递 id 。
<li data-toggle="modal" data-target="#myModal"><a href="posts.php?id=<?php echo $post_id; ?>" >See post</a></li>
<div id="myModal" class="modal fade" 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">Post</h4>
</div>
<div class="modal-body">
// include the posts.php here
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
最佳答案
我们可以使用ajax来做到这一点,请参阅下面的代码
在php文件中写入要显示的内容
$("#dynamicdata").click(function(){
var dyid=$(this).attr("posts_id");
$.ajax({
url:"posts.php",
type:"post",
dsta:{'id':dyid},
success:function(data){
$('#myModalbody').html('');
$("#mymodelbody").html(data);
}
});
});
<li data-toggle="modal" data-target="#myModal" posts_id="<?php echo $post_id; ?>" id="dynamicdata"><a href="#" >See post</a></li>
<div id="myModal" class="modal fade" 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">Post</h4>
</div>
<div class="modal-body" id="mymodelbody">
// include the posts.php here
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
关于javascript - 如何使用 id 显示此模式中的 .php 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41907982/