嗨,大家好我的ajax传递postID到我的模态有问题。 var pid获取我想要的id但是当我回显postID时它不会从ajax获取值。请帮忙
$(document).ready(function(){
$('#myModal').on('show.bs.modal', function (e) {
var pid = $(e.relatedTarget).data('id');
$.ajax({
type: 'post',
url: 'domain/modal.php',
data: 'post_id=' + pid,
success: function (r) {
}
});
});
});

<a class="link-to-post" data-toggle="modal" data-target="#myModal" name="modal" data-id="<?php echo get_the_ID(); ?>">Button</a>
<?php
$post_ID = $_POST['post_id'];
?>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-body">
<div class="row">
<div class="col-sm-6">
<?php echo $post_ID;?>
</div>
<div class="col-sm-6">
</div>
</div>
</div>
</div>
</div>
</div>
&#13;
答案 0 :(得分:0)
你能试试这段代码吗?
更改data: 'post_id=' + pid,
到data:{ 'post_id': pid },
在要添加ID的div中添加ID。类似的东西:
<div id="idContainer" class="col-sm-6"></div>
在您的成功功能中,您可以添加此
$('#idContainer').html(pid);