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

Posted

技术标签:

【中文标题】如何在关闭 Bootstrap 3 模式时重新加载页面【英文标题】:How to reload page on closing a Bootstrap 3 modal 【发布时间】:2014-03-01 22:43:34 【问题描述】:

我的目标是在 Bootstrap 模式关闭时重新加载页面。用户可以通过单击关闭按钮或图标或单击离开模式来关闭模式。

到目前为止,我的代码非常标准。取自: http://getbootstrap.com/javascript/#modals

<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch
</button>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">My title</h4>
      </div>
      <div class="modal-body">
        My content
      </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</button>
      </div>
    </div>
  </div>
</div>

如何在模式关闭后重新加载页面?

更新:哇,每个人的反应都非常快。谢谢

【问题讨论】:

【参考方案1】:

您可以将事件绑定到单击关闭时重新加载页面:

$('#myModal').on('hidden.bs.modal', function () 
 location.reload();
)

Demo

【讨论】:

如果modal是动态添加的,添加到文档中。 $(document).on('hidden.bs.modal', '#Control_id', function (event) // code to run on closing ); 来源:***.com/a/22058588/2495584【参考方案2】:

用你的按钮试试这个:

onclick="javascript:window.location.reload()"

完整按钮:

<button type="button" onclick="javascript:window.location.reload()" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>

示例: http://jsfiddle.net/Valtos/52VtD/2288/embedded/result/

【讨论】:

感谢您的建议 - 我想我将使用 Milind 的解决方案 'hidden.bs.modal' 因为它是 Bootstrap 提供的事件 这也是为了让我的脚本分开 - 而不是在按钮上有另一个属性。 这只是没有按预期工作,试试***.com/a/21578351/3063226 它对我来说非常有效。【参考方案3】:
$('#myModal').on('hidden', function () 
  document.location.reload();
)

Demo

【讨论】:

以上是关于如何在关闭 Bootstrap 3 模式时重新加载页面的主要内容,如果未能解决你的问题,请参考以下文章

如何在提交时重新加载页面

当我单击外部时,Bootstrap 5 静态模式仍然关闭

eModal 在 bootstrap 3 中加载 javascript 但在 bootstrap 4 中不起作用

带有 Bootstrap 模态视图的 Rails 应用程序,包含表单、提交和消失模态视图,无需重新加载页面

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

Bootstrap 3 - 从另一个模式打开模式