我有一个表格,可以用 Ajax 提交一次。响应也出现了一种形式,它取代了旧的形式。但是当我按下提交按钮时,表单不会使用 Ajax 提交,而是使用普通方法。为什么?这是我的 jQuery 代码:
<script>
$(document).ready(function () {
$('#ajax_form').submit(function(e) {
var form = $('#ajax_form');
var data = form.serialize();
$.post('game/write.php', data, function(response) {
console.log(response);
$('#power').replaceWith(response);
});
return false;
});
});
</script>
表格:
<div id="power">
<div class="span4">
<form action="game/write.php" id="ajax_form" method="post"><input type="hidden" value="1" name="button"><button type="submit" class="btn btn-block btn-success"><img src="images/null.png"></button></form>
<form action="game/write.php" id="ajax_form" method="post"><input type="hidden" value="4" name="button"><button type="submit" class="btn btn-block btn-success"><img src="images/null.png"></button></form>
<form action="game/write.php" id="ajax_form" method="post"><input type="hidden" value="7" name="button"><button type="submit" class="btn btn-block btn-success"><img src="images/null.png"></button></form>
</div>
<div class="span4">
<form action="game/write.php" id="ajax_form" method="post"><input type="hidden" value="2" name="button"><button type="submit" class="btn btn-block btn-success"><img src="images/null.png"></button></form>
<form action="game/write.php" id="ajax_form" method="post"><input type="hidden" value="5" name="button"><button type="submit" class="btn btn-block btn-success"><img src="images/null.png"></button></form>
<form action="game/write.php" id="ajax_form" method="post"><input type="hidden" value="8" name="button"><button type="submit" class="btn btn-block btn-success"><img src="images/null.png"></button></form>
</div>
<div class="span4">
<form action="game/write.php" id="ajax_form" method="post"><input type="hidden" value="3" name="button"><button type="submit" class="btn btn-block btn-success"><img src="images/null.png"></button></form>
<form action="game/write.php" id="ajax_form" method="post"><input type="hidden" value="6" name="button"><button type="submit" class="btn btn-block btn-success"><img src="images/null.png"></button></form>
<form action="game/write.php" id="ajax_form" method="post"><input type="hidden" value="9" name="button"><button type="submit" class="btn btn-block btn-success"><img src="images/null.png"></button></form>
</div>
这也回来了,只有 Buttoncolors 不同。