0

我有一个表格,可以用 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 不同。

4

2 回答 2

1

您需要事件委托 -

$(document).on('submit','#ajax_form',function(e) {

尝试这个 -

$(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;
});
于 2013-04-29T17:20:43.643 回答
-1

event.preventDefault()

文档:http ://api.jquery.com/event.preventDefault/

现在更新了您的代码,如下所示

 <script>
$(document).ready(function () {


  $('#ajax_form').click(function(event) {//use click instead of submit 

     event.preventDefault() // then use even.preventDefault()

    var form = $('#ajax_form');
    var data = form.serialize();
    $.post('game/write.php', data, function(response) {
        console.log(response);
        $('#power').replaceWith(response);
    });
    return false;
});

});
</script>

add event.preventDefault() [如果调用该方法,则不会触发事件的默认动作。]

于 2013-04-29T17:24:45.403 回答