我在頁面上使用了許多相同ID的表單。當我按任何形式的提交按鈕時,首先第一個將提交,第二個單擊第二個......之後。但是當我按下提交按鈕時,表單會提交按鈕所屬的位置。我怎樣才能做到這一點。使用JQuery Ajax提交特定表單
這裏我的JS代碼:
$(document).on('submit','#ajax_form',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;
});
這裏的HTML代碼:
<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>
不要使用重複的ID。永遠。 – 2013-05-01 15:33:54
您可以將您的提交按鈕命名爲唯一名稱,並檢查以哪種方式提交了哪個按鈕。並介意關於id的評論。他們需要在頁面中獨一無二。 – Daniel 2013-05-01 15:36:22