【问题标题】:How to reload page on closing a Bootstrap 3 modal如何在关闭 Bootstrap 3 模式时重新加载页面
【发布时间】:2025-11-26 07:15:02
【问题描述】:

我的目标是在 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>

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

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

【问题讨论】:

    标签: javascript jquery twitter-bootstrap twitter-bootstrap-3


    【解决方案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

    【讨论】:

      最近更新 更多