【问题标题】:How to add an event after close the modal window?关闭模态窗口后如何添加事件?
【发布时间】:2013-02-12 20:07:49
【问题描述】:

我有代码模式:

    <-- Button to trigger modal -->
<div id="result"></div>
<a href="#myModal" role="button" class="btn" data-toggle="modal">Launch demo modal</a>

<-- Modal -->
<div class="modal" id="myModal" 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 id="myModalLabel">Modal header</h3>
  </div>
  <div class="modal-body">
    <p>One fine body…</p>
  </div>
  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
    <button class="btn btn-primary">Save changes</button>
  </div>
</div>

我有代码什么时候应该在关闭 moal 窗口之后:

$('#result').html('yes,result');

请告诉我如何在关闭模式窗口(关闭或隐藏)时执行第二个代码?

【问题讨论】:

标签: jquery twitter-bootstrap modal-dialog


【解决方案1】:

如果您使用的是 Bootstrap 3.x 版,那么现在执行此操作的正确方法是:

$('#myModal').on('hidden.bs.modal', function (e) {
  // do something...
})

向下滚动到活动部分以了解更多信息。

http://getbootstrap.com/javascript/#modals-usage

无论何时发布第 4 版 (http://v4-alpha.getbootstrap.com/components/modal/#events),这似乎都保持不变,但如果是这样,我一定会用相关信息更新这篇文章。

【讨论】:

    【解决方案2】:

    我找到了答案。谢谢下一个正确的答案:

    $("#myModal").on("hidden", function () {
      $('#result').html('yes,result');
    });
    

    这里有活动http://bootstrap-ru.com/javascript.php#modals

    UPD

    对于 Bootstrap 3.x 需要使用 hidden.bs.modal:

    $("#myModal").on("hidden.bs.modal", function () {
      $('#result').html('yes,result');
    });
    

    【讨论】:

    • 我没有使用引导程序,但这有助于我在隐藏模式时运行代码:)
    【解决方案3】:

    可能有用的答案很少,尤其是当您有动态内容时。

    $('#dialogueForm').live("dialogclose", function() {
        //your code to run on dialog close
    });
    

    或者,在打开模式时,有一个回调。

    $( "#dialogueForm" ).dialog({
        autoOpen: false,
        height: "auto",
        width: "auto",
        modal: true,
        my: "center",
        at: "center",
        of: window,
        close : function() {
            // functionality goes here
        }
    });
    

    【讨论】:

    • .live() 已从 jQuery 1.7 中弃用并从 1.9 中删除
    【解决方案4】:
    $('.close').click(function() {
      //Code to be executed when close is clicked
      $('#result').html('yes,result');
    });
    

    【讨论】:

    • 这个答案与 Bootstrap 无关。
    【解决方案5】:

    先检查modal div是否合适,

    <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
      Launch demo modal
    </button>
    
    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      <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">Modal title</h4>
          </div>
          <div class="modal-body">
            ...
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>
    

    参考:https://jsfiddle.net/jakecigar/DTcHh/42400/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多