【问题标题】:I want to show modal popup before leaving the page我想在离开页面之前显示模式弹出窗口
【发布时间】:2019-02-03 18:21:33
【问题描述】:
<a href="https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_modal&stacked=h" id="leave">click here to leave the page</a>


<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Modal Header</h4>
      </div>
      <div class="modal-body">
        <p>Changes made may not be saved.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</div>

$("#leave").click(function() {
  $(window).bind('beforeunload', function() {
    return 'Changes you made may not be Saved';
  });
});

here is the js fiddle am working on

【问题讨论】:

  • 你不能那样做。
  • 我认为最接近你的是stackoverflow.com/questions/20253246/…。或者只是让您检查是否设置了“已保存”变量。
  • 您能否确认您是否需要那个“beforeunload”事件,或者它只是举例?如果您不需要它,则可以显示模型,否则不需要。

标签: javascript jquery html css model-view-controller


【解决方案1】:

您可以阻止leave 链接的默认行为,显示对话框并使用超时在几秒钟内重定向页面。

例子:

$("#leave").click(function(e) {
  e.preventDefault();
  var link = this.href;
  //code to show dialog
  window.setTimeout(function() {    
    window.location.href = link;
  }, 2000);
});

另外,您需要将脚本代码包装在script html 标记中。

【讨论】:

    【解决方案2】:

    为什么不能在模态按钮内部设置重定向逻辑,让a元素显示模态。这似乎是一个更简单的解决方案。

    $("#leave").click(e => {
        e.preventDefault()
        $("#myModal").modal({
            show: true
        })
    })
    $("#proceedLeaveBtn").click(() => {
        window.location.href = "https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_modal&stacked=h"
    })
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <a href="#" id="leave">click here to leave the page</a>
    
    <div id="myModal" class="modal fade" role="dialog">
        <div class="modal-dialog">
    
            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Modal Header</h4>
                </div>
                <div class="modal-body">
                    <p>Changes made may not be saved.</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                    <button id="proceedLeaveBtn" type="button" class="btn btn-default" data-dismiss="modal">Proceed</button>
                </div>
            </div>
    
        </div>
    </div>
    您甚至可以在按钮元素中指定data-href 属性并在点击处理程序中检索它。这取决于您。

    【讨论】:

      猜你喜欢
      • 2017-03-01
      • 2020-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多