Bootstrap 模式关闭按钮

Posted

技术标签:

【中文标题】Bootstrap 模式关闭按钮【英文标题】:Bootstrap modal close button 【发布时间】:2017-05-24 18:23:32 【问题描述】:

我对引导程序中的关闭模式有疑问。当我打开带有数据的模态窗口时(根据触发按钮改变模态内容)。这是http://getbootstrap.com/javascript/#modals中的例子

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">Open modal for @mdo</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@fat">Open modal for @fat</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@getbootstrap">Open modal for @getbootstrap</button>
...more buttons...

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
  <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="exampleModalLabel">New message</h4>
      </div>
      <div class="modal-body">
        <form>
          <div class="form-group">
            <label for="recipient-name" class="control-label">Recipient:</label>
            <input type="text" class="form-control" id="recipient-name">
          </div>
          <div class="form-group">
            <label for="message-text" class="control-label">Message:</label>
            <textarea class="form-control" id="message-text"></textarea>
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" id="btnclose" data-dismiss="modal">Close</button>
        <button type="button" id="btnsend" class="btn btn-primary">Send message</button>
      </div>
    </div>
  </div>
</div>

这没关系,但是如果我关闭模态并使用另一个按钮打开,当我单击发送按钮时,它会在控制台中打印第一个模态和此模态的接收者值。但我只需要打印最后一个收件人值。 我不明白为什么它将前一个模式关闭的事件堆叠起来。 这是我的两个按钮的 jQuery 代码:

    $('#exampleModal').on('show.bs.modal', function (event) 

    var button = $(event.relatedTarget) // Button that triggered the modal
    var recipient = button.data('whatever') // Extract info from data-* attributes
    // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
    // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
    var modal = $(this)
    modal.find('.modal-title').text('New message to ' + recipient)
    modal.find('.modal-body input').val(recipient)

    $("#btnsend").one("click", function()
        console.log('Pinchado')
      console.log(recipient)
    )

    $("#btnclose").one("click", function()
        console.log('Cerrando...')
    )

    $('#exampleModal').on('hidden.bs.modal', function (e) 
      console.log('Cerrada');
    )

  )

非常感谢你,我希望有人能帮助我。 https://jsfiddle.net/DTcHh/28480/

【问题讨论】:

对我来说只打印最后一个模态收件人。 【参考方案1】:

这是您执行此操作的旧方式,或者您可以说是传统方式。它仍然有答案。只需在显示事件绑定之外绑定隐藏和单击事件。

$('#exampleModal').on('show.bs.modal', function(event) 

   var button = $(event.relatedTarget) // Button that triggered the modal
   var recipient = button.data('whatever') // Extract info from data-* attributes
     // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
     // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
   var modal = $(this)
   modal.find('.modal-title').text('New message to ' + recipient)
   modal.find('.modal-body input').val(recipient)
 )
 
 $("#btnsend").on("click", function() 
     console.log('Pinchado')
     console.log(recipient)
   )

   $("#btnclose").on("click", function() 
     console.log('Cerrando...')
   )

   $('#exampleModal').on('hidden.bs.modal', function(e) 
     console.log('Cerrada');
   )
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/css/bootstrap-dialog.min.css">

<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">


<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/js/bootstrap-dialog.min.js"></script>

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">Open modal for @mdo</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@fat">Open modal for @fat</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@getbootstrap">Open modal for @getbootstrap</button>
...more buttons...

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
  <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="exampleModalLabel">New message</h4>
      </div>
      <div class="modal-body">
        <form>
          <div class="form-group">
            <label for="recipient-name" class="control-label">Recipient:</label>
            <input type="text" class="form-control" id="recipient-name">
          </div>
          <div class="form-group">
            <label for="message-text" class="control-label">Message:</label>
            <textarea class="form-control" id="message-text"></textarea>
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" id="btnclose" data-dismiss="modal">Close</button>
        <button type="button" id="btnsend" class="btn btn-primary">Send message</button>
      </div>
    </div>
  </div>
</div>

下面给出的是现代的做法。 动态创建modal的方式可以参考this link。

function open_modal(name) 

  var message = $('#frm_1');
  BootstrapDialog.show(
    title: 'New message to ' + name,
    message: $('#frm_1'),
    onshown : function() 
      $('#recipient-name').val(name);
    ,
    onhide : function(dialog) 
      $('#hidden-div').append(message);
    ,
    buttons: [
      label: 'Close',
      action: function(dialog) 
        dialog.close();
      
    , 
      label: 'Send message',
      cssClass: 'btn btn-primary',
      action: function(dialog) 
        // Do whatever send message does, here
      
    ]
  );



<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/css/bootstrap-dialog.min.css">

<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">


<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/js/bootstrap-dialog.min.js"></script>

<button type="button" class="btn btn-primary" onclick="open_modal('@mdo')">Open modal for @mdo</button>
<button type="button" class="btn btn-primary" onclick="open_modal('@fat')">Open modal for @fat</button>
<button type="button" class="btn btn-primary" onclick="open_modal('@getbootstrap')">Open modal for @getbootstrap</button>

<div id="hidden-div" style="display : none">

  <form id="frm_1">
    <div class="form-group">
      <label for="recipient-name" class="control-label">Recipient:</label>
      <input type="text" class="form-control" id="recipient-name">
    </div>
    <div class="form-group">
      <label for="message-text" class="control-label">Message:</label>
      <textarea class="form-control" id="message-text"></textarea>
    </div>
  </form>


</div>

【讨论】:

【参考方案2】:

在引导程序 3.3.7 中 这对我有用:

$('#myModal').trigger('click.dismiss.bs.modal')

【讨论】:

以上是关于Bootstrap 模式关闭按钮的主要内容,如果未能解决你的问题,请参考以下文章

如何在关闭 Bootstrap 3 模式时重新加载页面

提交后如何关闭引导模式?

Bootstrap 4 - 避免屏幕点击的模式关闭

如果第二次,Bootstrap Modal 按钮关闭无法关闭

Twitter Bootstrap / jQuery - 如何暂时防止模式被关闭?

在 Bootstrap 模式中单击按钮时调用 JS 函数