【问题标题】:Bootstrap modal not closing after form submission表单提交后引导模式未关闭
【发布时间】:2015-08-18 10:08:48
【问题描述】:

我相信我会在这里得到一个解决方案,引导模式在表单提交后不会消失。 我只想让引导模式在表单提交后消失,当再次单击按钮(不刷新页面)时,表单应该打开,发送数据和模式应该消失等等......就是这样!

这是一个引导模式:

<div class="modal fade myPopup" id="basicModal" tabindex="-1" role="dialog" 
aria-labelledby="basicModal" aria-hidden="true">
  <div class="modal-dialog" id="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="myModalLabel">Add New Board</h4>
      </div>
      <div class="modal-body">
        <form class="create_Category_board" id="myform">
          <input type="text" 
                 id="customInput"
                 class="board_name"
                 name="board[name]"
                 placeholder="Board Name ..." />
          <input type="text"
                 class="board_description"
                 name="board[description]"
                 placeholder="Board Description ..." />
          <input type="hidden"
                 class="board_description"
                 id="myCid"
                 name="board[category_id]" />
          <input type="submit" value="Create Board" class="ShowFormButton"/>
        </form>
      </div>
      <div class="modal-footer">

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

我的表单绑定了主干JS函数:

events: {

    "submit form.create_Category_board": "createCategoryBoard"
  },

  createCategoryBoard: function(event) {
    event.preventDefault();
    var that = this;

    // get form attrs, reset form
    var $form = $(event.target);
    var attrs = $form.serializeJSON();

    $form[0].reset();

    var board = new Kanban.Models.Board();

    // fail if no board name
    if (!attrs.board.name) {
      var $createContainer = $("div.create_board");
      var $nameInput = that.$el.find("input.board_name");

      $createContainer.effect("shake", {
        distance: 9,
        times: 2,
        complete: function() {
          $nameInput.show();
          $nameInput.focus();
        }
      }, 350);
      return false;
    }

    attrs.board.category_id = myid;
    var category = Kanban.categories.get(myid);
    var boards = category.get("assigned_boards");
    // save list
    board.save(attrs.board, {
      success: function(data) {
        board.get("users").add(Kanban.currentUser);

        boards.add(board);

        // keep focus on list input
        that.$el.find("input.board_name").focus();

      }
    });
    $('#basicModal').modal('hide');
  }

作为上述函数的最后一行,我尝试了网络上最流行的解决方案$('#basicModal').modal('hide');!它使我的模态看起来像这样:

即它不隐藏模态,而且使屏幕变黑并将引导模态的方向从屏幕中心更改为小右。可能是被 JS 的 CSS 覆盖了。但我不确定。

【问题讨论】:

  • 你能做一个工作的plnkr吗?
  • @PraveenKumar 我不知道如何在 plnkr 中包含主干代码!
  • 通过新建文件上传所有文件。
  • @PraveenKumar 我已将其添加到 plnkr。你能看到吗:plnkr.co/edit/YIjF32ojGVHslrnwr5ZP?p=preview。我对这个工具不太熟悉!我想我又错过了一些东西!
  • 嘿@EM923 它的工作方式与您在家中的工作方式一样吗?

标签: javascript jquery css twitter-bootstrap backbone.js


【解决方案1】:

我发布自己的答案是因为如果有人需要更快的帮助,那么它可能会有所帮助: 只需将:$('#basicModal').modal('hide'); 替换为以下行:

$(".modal-header button").click(); 这是我的JS 函数的最后一行。 干杯:)

【讨论】:

    【解决方案2】:

    问题是隐藏模式后 div.modal-backdrop 仍然存在, 您可以在开发人员工具中看到它。尝试简单删除 $('#basicModal').modal('hide'); $('.modal-backdrop').remove();

    干杯

    【讨论】:

    • 当我再次点击打开模态(不刷新页面)时发生了什么?它不会出现,因为它已被删除!如何让它再次出现在 DOM 中?
    猜你喜欢
    • 2021-12-23
    • 2014-06-22
    • 1970-01-01
    • 2014-06-30
    • 2015-06-27
    • 2021-08-02
    • 1970-01-01
    • 2018-07-15
    • 2016-02-02
    相关资源
    最近更新 更多