jQuery 关闭模态窗口后如何添加事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14840870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:59:25  来源:igfitidea点击:

How to add an event after close the modal window?

jquerytwitter-bootstrapmodal-dialog

提问by Leo Loki

I have code modal:

我有代码模式:

    <-- 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>

And i have code when should been after close moal window:

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

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

Tell me please how to make that when closing the modal window(close or hide) executed a second code ?

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

回答by MattD

If you're using version 3.x of Bootstrap, the correct way to do this now is:

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

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

Scroll down to the events section to learn more.

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

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

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

This appears to remain unchanged for whenever version 4 releases (http://v4-alpha.getbootstrap.com/components/modal/#events), but if it does I'll be sure to update this post with the relevant information.

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

回答by Leo Loki

I find answer. Thanks all but right answer next:

我找到答案。非常感谢接下来的正确答案:

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

Events here http://bootstrap-ru.com/javascript.php#modals

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

UPD

UPD

For Bootstrap 3.x need use hidden.bs.modal:

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

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

回答by PodTech.io

Few answers that may be useful, especially if you have dynamic content.

可能有用的答案很少,特别是如果您有动态内容。

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

Or, when opening the modal, have a callback.

或者,当打开模态时,有一个回调。

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

回答by Huy

$('.close').click(function() {
  //Code to be executed when close is clicked
  $('#result').html('yes,result');
});