javascript - Bootstrap 模态表单在验证之前关闭

标签 javascript jquery forms twitter-bootstrap

我使用此处的示例来构建我的表单:http://bootply.com/60244

我的 JavaScript 似乎没有被调用,因此一旦我单击“保存更改”,表单就会关闭。任何想法将不胜感激

这是我的代码

    <!-- Modal -->
    <div class="modal hide fade" id="mailerModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
          <h3_pink id="myModalLabel">Request a Mailer Bag</h3_pink>
        </div>
        <div class="modal-body">
            <h3_pink> </h3>
            <form method="post" id="FormMailer" action="soon2.php">
                <input type="hidden" name="FormMailer" value="1">
                <table>
                    <tbody>
                        <tr><td>First Name</td><td><input type="text" name="contact_fname" id="contact_fname" style="width:90%"><span class="hide help-inline">This is required</span></td>
                        <td>Last Name</td><td><input type="text" name="contact_lname" id="contact_lname" placeholder="Last Name" style="width:90%"><span class="hide help-inline">This is required</span></td></tr>
                        <tr><td>Email Address</td><td colspan="3"><input type="text" name="contact_email" id="contact_email" placeholder="Email Address" style="width:90%"><span class="hide help-inline">This is required</span></td></tr>
                        <tr><td>Street1</td><td colspan="3"><input type="text" id="contact_street1" name="contact_street1" placeholder="Street" style="width:90%"><span class="hide help-inline">This is required</span></td></tr>
                        <tr><td>Street2</td><td colspan="3"><input type="text" id="contact_street2" name="contact_street2" placeholder="Street" style="width:90%"></td></tr>
                        <tr><td>City</td><td colspan="3"><input type="text" id="contact_city" name="contact_city" placeholder="City" style="width:90%"><span class="hide help-inline">This is required</span></td></tr>
                        <tr><td>Zip</td><td colspan="3"><input type="text" name="zip" id="zip" style="width:90%"><span class="hide help-inline">This is required</span></td></tr>
                        <tr><td>Message</td><td colspan="3"><textarea id="contact_message" name="contact_message" style="width:90%"></textarea></td></tr>
                    </tbody>
                </table>
            </form>
        </div><!-- /.modal-body -->
        <div class="modal-footer">
            <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
            <button class="btn btn-primary" data-dismiss="modal" id="FormMailerSubmit">Save changes</button>
        </div>
    </div><!-- /.modal -->
    <!-- Modal Ends -->

    <script src="https://code.jquery.com/jquery.js"></script>
    <script src="/soon/js/bootstrap.js"></script>
    <link rel="stylesheet" href="soon/styles/bootstrap.min.css" media="screen"  />
    <link rel="stylesheet" href="soon/styles/bootstrap-responsive.min.css" media="screen"  />
    <link rel="stylesheet" href="soon/styles/style.css"/>
    <link rel="stylesheet" href="soon/styles/style_responsive.css"/>

    <script type='text/javascript'>
        $(document).ready(function() {
            $('#FormMailerSubmit').click(function(){
            if ($('#contact_fname').val()==="") {
                  // invalid
                  $('#contact_fname').next('.help-inline').show();
                  return false;
                }
                else {
                  // $('#FormMailer').submit();

                  return true;
                }
            });
        });           
    </script>

最佳答案

由于按钮上的 data-dismiss="modal" 属性而关闭。删除它,它就不会关闭。如果您需要在 JS 中关闭它,可以使用 modal.modal('hide');

关于javascript - Bootstrap 模态表单在验证之前关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21212426/

相关文章:

javascript - 如何截取一个<div>,里面有一个div标签&lt;iframe&gt;?

javascript - Google App Engine Standard Java 在部署时使用 Unicode 字符破坏代码

asp.net - 在部署时将本地 (JQuery) 链接转换为 CDN 链接

javascript - li rel=选择器

javascript - 间接单击提交按钮不会设置 $_POST ['submit' ]

javascript - 我怎样才能改进这个 WebGL/GLSL 图像下采样着色器

javascript - 读取文本文件以在 HTML 中显示

javascript - 单选按钮/复选框未使用 AJAX 输入 MySQL DB

javascript - 提交表单并在没有 jQuery 的情况下获得 JSON 响应

html - 您可以将数据列表与 HTML 文本区域一起使用吗?