jQuery正在阻止表单提交

时间:2018-11-13 23:45:51

标签: javascript jquery html css html5

我使用Bootstrap 4和PHPMailer创建了一个表单提交,在我添加一些jQuery尝试显示警告框以显示成功或失败之前,该表单一直运行良好。

`

                         <div id="messages" class="hide" role="alert">
          <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <div id="messages_content"></div>
        </div>


                        <form id="form" role="form" action="test.php" method="post" enctype="multipart/form-data" class="form">
                            <div class="row">
                                <div class="col-md-3">
                                    <label class="input-group g-rounded-3 g-mb-0--md">
                                        <input class="form-control g-brd-white g-brd-right-none border border-secondary border-right-0 u-has-success-v1-2" type="text" placeholder="Your name" name="name" required>
                                        <div class="input-group-append">
                                            <span class="input-group-text g-bg-white g-color-gray border border-secondary border-left-0"><i class="fa fa-user"></i></span>
                                        </div>
                                    </label>
                                </div>
                                <div class="col-md-3">
                                    <label class="input-group g-rounded-3 g-mb-0--md">
                                        <input class="form-control g-brd-white g-brd-right-none border border-secondary border-right-0" type="tel" placeholder="Your phone number" name="number" required>
                                        <div class="input-group-append">
                                            <span class="input-group-text g-bg-white g-color-gray border border-secondary border-left-0"><i class="fa fa-phone"></i></span>
                                        </div>
                                    </label>
                                </div>
                                <div class="col-md-4">
                                    <select class="custom-select w-100 mr-sm-3 mb-3 mb-lg-0 border border-secondary" id="inlineFormCustomSelect" name="service">
                                        <option selected="">Choose Services...</option>
                                        <option value="General Enquiry">General Enquiry</option>
                                        <option value="Light Demolition">Light Demolition</option>
                                        <option value="Rubbish Clearance">Rubbish Clearance</option>
                                        <option value="House Clearance">House Clearance</option>
                                    </select>
                                </div>
                                <div class="col-md-2">
                                    <button class="btn btn-md u-btn-white text-uppercase g-font-size-12 g-font-weight-600 g-rounded-5 border border-secondary bg-white btn-block" type="submit">Request Call Back</button>
                                </div>
                            </div>
                        </form>

                  </section>


                </div>
            </div>
        </section>



        <script src="http://code.jquery.com/jquery.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

    <script>
        $('#form').submit(function(e) {
            $('#messages').removeClass('hide').addClass('alert alert-success alert-dismissible').slideDown().show();
            $('#messages_content').html('<h4>MESSAGE HERE</h4>');
            $('#modal').modal('show');
            e.preventDefault();
        });
    </script>`

提交表单后,将显示警告框,其中文本字段将保持填充状态(显然不会在任何地方发送)并且不会接收电子邮件。

如果我删除了jQuery,则将表单发送到php文件,进行处理并返回该页面,并收到电子邮件。我似乎找不到此特定问题,我看不到该问题!

1 个答案:

答案 0 :(得分:2)

问题出在jQuery的底部:

e.preventDefault(); 

这防止了表单提交事件的默认操作,因此表单不会提交。删除此行,它将起作用。