我在使用 ajax 回调时遇到了一些问题,希望能得到一些帮助。基本上,这个脚本用archive.php替换了#inbox div(通常会生成消息和匹配的模态,但为了简单起见,我只包含了通用模态)。
关闭模式时会触发 ajax 回调。它当前返回模态 ID。但是,这只适用于一次 - 这就是我遇到问题的地方。问题似乎出在 jQuery html 命令上。我已经用 alert(id) 替换了它,脚本将运行任意多次。有什么建议么?如果我需要澄清更多,请告诉我。谢谢。
HTML / JavaScript:
<div id="inbox"> <!-- Content should be put between this div -->
<?php include_once('archive.php'); ?>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="assets/js/bootstrap.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#inbox .modal').on('hidden', function() {
id = $(this).attr('id');
$.ajax({
type: "POST",
url: "archive.php",
data: {message_id : id},
cache: false,
success: function(data) {
$('#inbox').html(data); // problem is here
}
});
});
});
</script>
PHP(存档.php):
<?php echo $_POST['message_id']; // I've also used SQL inserts, which work fine when I'm not using the jQuery HTML command ?>
<!-- Modal 1 -->
<a href="#modalOne" role="button" class="btn" data-toggle="modal">Launch demo modal</a>
<div id="modalOne" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Modal header</h3>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button class="btn btn-primary">Save changes</button>
</div>
</div>
<!-- Modal 2 -->
<a href="#modalTwo" role="button" class="btn" data-toggle="modal">Launch demo modal</a>
<div id="modalTwo" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Modal header</h3>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button class="btn btn-primary">Save changes</button>
</div>
</div>
<!-- Modal 3 (etc...) -->