【问题标题】:How can I use a bootstrap-modal multiple times?如何多次使用引导模式?
【发布时间】:2018-01-03 15:39:47
【问题描述】:

如何在默认状态下多次使用动态生成的 Bootstrap-Modal?

我的问题是:当我点击按钮打开模态时,可以看到默认值 1 和 2。现在我在上方输入字段中输入“3”,用“保存更改”或“关闭模态”关闭”并再次按下按钮。现在,输入字段中应该有 1 和 2,但它仍然是 3 和 2。

我尝试在 jquery 中修复它(正如您在代码中看到的那样),但它不起作用:

    $('#openModal1').on('click', function(){
      let x = `
    <form class="modal fade" id="modal-form" tabindex="-1" role="dialog" aria-labelledby="modal-dialogLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
          <div class="modal-content">
              <div class="modal-header">
                  <h5 class="modal-title" id="modal-dialogLabel">Modal title</h5>
                  <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                  </button>
              </div>
              <div class="modal-body">

                  <!-- actual form markup -->
                  <div class="form-group">
                      <label for="field1">Example label</label>
                      <input name="field1" type="text" value="1" class="form-control" id="field1" placeholder="Example input">
                  </div>
                  <div class="form-group">
                      <label for="field2">Another label</label>
                      <input name="field2" type="text" value="2" class="form-control" id="field2" placeholder="Another input">
                  </div>
                  <!-- /actual form markup -->

              </div>

              <div class="modal-footer">
                  <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                  <button id="save" data-dismiss="modal" type="submit" class="btn btn-primary">Save changes</button>
              </div>
          </div>
      </div>
  </form>    
    `;
      $("#modal-area").append(x);
      $('#modal-form').modal({});
    });

    $('#modal-form').on('hidden.bs.modal', function (event) {
        $("#modal-area").empty();
    });
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet"/>
  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js"></script>
  
    <div id="modal-area"></div>

    <button id="openModal1" type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal-form">
      Open modal form
  </button>

我该如何解决这个问题?

【问题讨论】:

  • 看看here
  • 您必须重置内容或重新加载。根据您生成(支持)它的方式,大小和复杂性将决定最佳方法。

标签: javascript jquery html twitter-bootstrap bootstrap-modal


【解决方案1】:

您需要使用html() 而不是append() 见下文

    $('#openModal1').on('click', function(){
      let x = `
    <form class="modal fade" id="modal-form" tabindex="-1" role="dialog" aria-labelledby="modal-dialogLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
          <div class="modal-content">
              <div class="modal-header">
                  <h5 class="modal-title" id="modal-dialogLabel">Modal title</h5>
                  <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                  </button>
              </div>
              <div class="modal-body">

                  <!-- actual form markup -->
                  <div class="form-group">
                      <label for="field1">Example label</label>
                      <input name="field1" type="text" value="1" class="form-control" id="field1" placeholder="Example input">
                  </div>
                  <div class="form-group">
                      <label for="field2">Another label</label>
                      <input name="field2" type="text" value="2" class="form-control" id="field2" placeholder="Another input">
                  </div>
                  <!-- /actual form markup -->

              </div>

              <div class="modal-footer">
                  <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                  <button id="save" data-dismiss="modal" type="submit" class="btn btn-primary">Save changes</button>
              </div>
          </div>
      </div>
  </form>    
    `;
      $("#modal-area").html(x);
      $('#modal-form').modal({});
    });

    $('#modal-form').on('hidden.bs.modal', function (event) {
        $("#modal-area").empty();
    });
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet"/>
  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js"></script>
  
    <div id="modal-area"></div>

    <button id="openModal1" type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal-form">
      Open modal form
  </button>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2017-09-11
    • 2018-02-01
    相关资源
    最近更新 更多