【问题标题】:Preventing parent page from refresh防止父页面刷新
【发布时间】:2016-08-12 04:59:56
【问题描述】:

当我单击弹出窗口的提交按钮时,我在 asp.net 中创建了弹出窗口,我的父页面得到刷新。我怎样才能防止它刷新。我已经编写了代码来在关闭事件时刷新页面,但在提交时没有,但在提交时会刷新。

`function closeMe() {
            window.close();
    }

    window.onload = function () {
        window.opener.document.body.disabled = true;
    }

    window.onbeforeunload = function () { myUnloadEvent(); }
    function myUnloadEvent() {
        window.opener.location.href = '../ContentPages/QuoteBuy.aspx';
    }`

谢谢。

【问题讨论】:

  • 如何打开弹出窗口?你能分享那个代码吗?

标签: javascript asp.net


【解决方案1】:

提交按钮总是向服务器发送请求,所以默认刷新页面。 为了克服这个问题,您可以将其定义为 simple button 或者您在 Jquery Click event 代码中有以下解决方案:

(1) return false; //at last statement of your code which not allow page to refresh
(2) use event.PreventDefault()

【讨论】:

    【解决方案2】:

    window.onbeforeunload 也会在 submit 的情况下被触发,因此将 location.href 设置为在 window.close() 之前刷新父页面。

    function closeMe() {
            window.opener.location.href = '../ContentPages/QuoteBuy.aspx';
            window.close();
        }
    
        window.onload = function () {
            window.opener.document.body.disabled = true;
        }
    
        window.onbeforeunload = function () { myUnloadEvent(); }
        function myUnloadEvent() {
            //Do your work
            //window.opener.location.href = '../ContentPages/QuoteBuy.aspx';
        }
    

    【讨论】:

      猜你喜欢
      • 2013-12-25
      • 1970-01-01
      • 1970-01-01
      • 2011-05-22
      • 2010-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-18
      相关资源
      最近更新 更多