繁体   English   中英

ASP.NET Post-Back和window.onload

[英]ASP.NET Post-Back and window.onload

我有一个检查某些输入字段是否更改的函数:

var somethingchanged = false;
$(".container-box fieldset input").change(function() {
   somethingchanged = true;
});

还有一个在window.onload上等待并触发的函数:

window.onbeforeunload = function(e) {
    if (somethingchanged) {
        var message = "Fields have been edited without saving - continue?";
        if (typeof e == "undefined") {
            e = window.event;
        }
        if (e) { 
             e.returnValue = message;
        }
        return message;
    }
}

但是,如果我编辑某些字段并单击“保存”按钮,则会触发事件,因为存在回发并且字段已被编辑。 无论如何,因此单击“保存”按钮后不会触发该事件吗?

谢谢

当我执行此模式时,页面上将显示showDirtyPrompt。 然后,每当发生不想进行脏检查的操作时,我都将变量设置为false。 您可以在按钮的客户端单击事件上执行此操作。

这样做的好处是,在其他情况下,您可能不想提示您,用户可能有其他按钮,例如进行其他回发。 这样,您的脏检查功能不必检查多个按钮,您就可以将责任推倒。

<input type="button" onclick="javascript:showDirtyPrompt=false;".../>

function unloadHandler()
{
   if (showDirtyPrompt)
   { 
    //have your regular logic run here
   }
   showDirtyPrompt=true;
}

是。 检查以确保单击的按钮不是保存按钮。 所以可能是这样的

if ($this.id.not("savebuttonID")) {
    trigger stuff
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM