donglu9978 2014-03-25 20:12
浏览 200
已采纳

bootstrap处理ajax表单提交

I have been trying to get this form to load for a couple days now, but all I can get is the form to submit to a page (not what I want, I want the form to submit and then close the modal and then put a success message from news.php into the div "thanks" holder).

Can anyone spot the issue I am having where it will one load the "news.php" page?

<div id="thanks"></div>    
<div class="modal fade" id="newsModal" tabindex="-1" role="dialog" aria-labelledby="newsModalLabel" aria-hidden="true">
                  <div class="modal-dialog">
                    <div class="modal-content">
                      <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="newsModalLabel">Add a News Post</h4>
                      </div>
                      <div class="modal-body">
                                <form class="addNew" role="form" method="POST" action="news.php">
                                  <div class="form-group">
                                    <input type="text" name="title" class="form-control" id="title" placeholder="Enter title here...">
                                  </div>
                                  <div class="form-group">
                                    <textarea class="form-control" name="message" rows="7" placeholder="Enter news post here..."></textarea>
                                  </div>
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-primary" id="addNews">Add News Post</button>
                        </form>
                      </div>
                    </div>
                  </div>
                </div>

and the javascript is:

<script type="text/javascript">
        $(document).ready(function () {
            $("button#addNews").click(function(){
                $.ajax({
                    type: "POST",
                    url: "news.php",
                    data: $('form.addNew').serialize(),
                    success: function(msg){
                        $("#thanks").html(msg) //hide button and show thank you
                        $("#newsModal").modal('hide');
                    },
                    error: function(){
                        alert("failure");
                    }
                });
            });
        });
  </script>
  • 写回答

2条回答 默认 最新

  • duanraotun1674 2014-03-25 21:17
    关注

    You didn't pass the event object to your click binding :

    <script type="text/javascript">
    $(document).ready(function (e) { // pass the event object
        $("button#addNews").click(function(){
            e.preventDefault(); // disable the POST of the form by the submit button
                $.ajax({
                type: "POST",
                url: "news.php",
                data: $('form.addNew').serialize(),
                success: function(msg){
                    $("#thanks").html(msg) //hide button and show thank you
                    $("#newsModal").modal('hide');
                },
                    error: function(){
                    alert("failure");
                }
            });
        });
    });
    

    Oops, I forgot the BootPly of my test. I just replaced your POST request to a GET one, I don't know how to make POST request inside BootPly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料