【问题标题】:Bootstrap modal stay open after form submit and page refresh表单提交和页面刷新后,引导模式保持打开状态
【发布时间】:2016-02-29 15:09:16
【问题描述】:

我有一个引导模式表单,用于发送 4 字段消息。按下提交按钮后,我想向用户显示“谢谢”消息。但不是我的表单正在关闭,页面正在刷新。

如何让模态表单保持打开状态,以便在提交按钮下方显示消息。

谢谢!

      <section class="modal-form">
  <!-- Modal Video first page -->
  <div class="modal fade" id="participa-modal" tabindex="-1" role="dialog" aria-labelledby="participa-modal">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title" id="myModalLabel">Participă la un demo!</h4>
        </div>
        <div class="modal-body">
          <div class="form-group">
            <form id="form" method="post" class="contact" action="" enctype="multipart/form-data" data-parsley-validate>
                <div class="row">
                  <div class="col-md-6">
                    <div class="form-group">
                      <input type="text" name="homepage_firstname" id="homepage_firstname" class="form-control-contact" placeholder="Nume" required>
                    </div>
                    <div class="form-group">
                      <input type="email" name="homepage_email" id="homepage_email" class="form-control-contact" placeholder="Email" required>
                    </div>
                    <div class="form-group">
                      <input type="text" name="homepage_phone" id="homepage_phone" class="form-control-contact" placeholder="Telefon" data-parsley-type="number" minlength="10" maxlength="10" required>
                      <input type="hidden" name="inner_message" id="inner_message" value="Participare demo curs!">
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="form-group">
                      <textarea class="form-control-contact" name="homepage_message" id="homepage_message" placeholder="Scrisoare de intentie"></textarea>
                    </div>
                  </div>
                </div>
                <div class="form-actions">
                    <input type="hidden" name="homepagesubmitted" value="TRUE" />
                    <button type="submit" class="btn orange sign-in-btn">Înscrie-te</button>
                </div>
                <?php echo $homepage_send ?>
              </form>
          </div>
        </div>
      </div>
    </div>
  </div>
  <!-- End Modal Video first page -->
  </section>

更新: 好的。所以我设法使它工作在下面的代码$(function () { var frm = $('#participa-modal'); frm.submit(function (ev) { $.ajax({ type: frm.attr('method'), url: frm.attr('action'), data: frm.serialize(), success: function (data) { alert('ok'); location.reload(); } }); ev.preventDefault(); }); });

问题是我如何替换 alert('ok') 以在 SEND 按钮下的同一个弹出窗口中表示感谢。

【问题讨论】:

  • 你能给你看一下标记吗?
  • 请提供您的代码。
  • 我已经发布了我的代码。/
  • 我使用了 bootstrap modal 形式。
  • $("form").submit(function () { $('#myModal').modal('show'); });..?

标签: javascript jquery html forms twitter-bootstrap


【解决方案1】:
$(".modal-body form").submit(function(e) {
    var url = "ActionScript.php"; // the script where you handle the form input.
    $.ajax({
           type: "POST",
           url: url,
           data: $(this).serialize(), // serializes the form's elements.
           success: function(data) {
               $(this).html("Thank you!!!");
           }
         });
    e.preventDefault(); // avoid to execute the actual submit of the form.
});

检查上面的代码以满足您的要求

使用的条款

$(".modal-body form") 类和子表单选择器,以定位您提交的form

.submit( 在您的表单的submit 上触发事件

type: "POST" 用于安全遍历数据,通过方法 post

.serialize()是收集(组装)发帖数据

success: 是一个回调,以便在提交成功时继续。

【讨论】:

  • 没关系,我之前尝试过,但我不想将感谢消息显示为 javascript pop-ul,而是将其显示为提交下方的 html div按钮。实际上我想要做的是提交表单 - 刷新页面并显示感谢消息而不关闭表单。
  • 所以您需要与问题伙伴@AdrianAdr 保持清楚。所以你需要在这里应用条件类型的东西..在提交表单后重定向到带有一些附加查询字符串的同一页面,例如。 ?action=提交。请求动作,检查条件,如果(动作==提交){显示你的html}其他{显示正常的},显示模态表单你可以使用jQuery,$(“#participa-modal”).modal(“ show");,谢谢!!!
猜你喜欢
  • 2014-08-29
  • 1970-01-01
  • 2019-05-19
  • 2012-11-08
  • 2015-05-30
  • 2016-07-03
  • 1970-01-01
  • 2020-10-06
  • 1970-01-01
相关资源
最近更新 更多