使用JQuery Ajax多次提交表单而无需重新加载页面

时间:2013-04-29 17:19:24

标签: html ajax forms jquery

我有一个表单,可以用Ajax提交一次。响应也是一种形式,它取代旧的。但是,当我按下提交按钮时,表单不会使用Ajax提交,并采用常规方法。为什么? 这是我的JQuery-Code:

<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是不同的。

2 个答案:

答案 0 :(得分: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;
});

答案 1 :(得分:-1)

<强> event.preventDefault()

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

现在更新您的代码,如bellow

 <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>

添加event.preventDefault()[如果调用此方法,则不会触发事件的默认操作。 ]