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

时间:2021-12-22 11:44:59

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:

我应该在关闭的莫尔窗口之后有代码:

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

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

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

4 个解决方案

#1


43  

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

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),这似乎保持不变,但如果确实如此,我将确保使用相关信息更新此帖子。

#2


19  

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

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');
});

#3


3  

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
              }  
              });

#4


0  

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

#1


43  

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

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),这似乎保持不变,但如果确实如此,我将确保使用相关信息更新此帖子。

#2


19  

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

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');
});

#3


3  

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
              }  
              });

#4


0  

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