我遇到了一些Ajax回調的問題,我希望能得到一些幫助。基本上,這個腳本用archive.php替換了#inbox div(通常會生成消息和匹配模式,但爲了簡單起見,我只包含了通用模式)。ajax成功函數(使用html方法)只觸發一次
當您關閉模式時,會觸發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(archive.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...) -->
完美地工作,謝謝! – scruwys