单击“提交"按钮时,警告用户有关未保存的数据脚本不会停止 [英] Warn Users About Unsaved Data Script Does Not Stop When Submit Button is Clicked

查看:98
本文介绍了单击“提交"按钮时,警告用户有关未保存的数据脚本不会停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C#编写的.net Web表单.为了警告用户他们可能有未保存的数据,已包含此脚本.

I have a .net web form written in C#. To warn users that they may have unsaved data, this script was included.

<script type="text/javascript">
	<!--
    
    var warnMessage = "You have entered new data on this page.  If you navigate away from this page without first saving your data, the changes will not be saved. To save your data, click the Cancel button, then save your data before leaving this page.";
   
    var g_isPostBack = false;
    
    function windowOnBeforeUnload() {
        if (g_isPostBack == true)
            return; // Let the page unload

        if (window.event)
            window.event.returnValue = warnMessage; // IE
        else
            return warnMessage; // FX
    }

    window.onbeforeunload = windowOnBeforeUnload;	
    
   //->
</script>



该脚本可以很好地工作,只是单击提交"按钮后脚本不会触发.发生的是g_isPostBack设置为false.关于如何关闭提交"按钮的回发的任何想法都很好.



The script works great, except its supposed to not fire when the Submit button is clicked. What happend is g_isPostBack is set to false instead. Any ideas on how to turn off the postback for the submit button would be great.

推荐答案

我相信,对提交调用的onclick事件进行简单的javascript函数调用可以设置g_isPostBack为true会做需要的.

此外,还有更多的自定义jquery插件可用,例如

脏表单 [ ^ ]
i believe a simple javascript function call on onclick event of submit call that sets the g_isPostBack to true will do the needful.

further there are more customized jquery plugins available like

Dirty Forms[^]


我做了两件事使警告生效.
1)我将警告脚本更改为此
I did 2 things to make the warnings work.
1) I changed the warning script to this
<script type="text/javascript">
	<!--   
    var warnMessage = "You have entered new data on this page.  If you navigate away from this page without first saving your data, the changes will not be saved. To save your data, click the Cancel button, then save your data before leaving this page.";
      
    var g_isPostBack = false;

    function windowOnBeforeUnload() {
        if (g_isPostBack == true)
            return; // Let the page unload

        if (window.event)
            window.event.returnValue = warnMessage; // IE
        else
            return warnMessage; // FX
    }



2)我在页面上添加了一个更新面板.



2) I added a updatepanel to the page.

0 points awarded.
    window.onbeforeunload = windowOnBeforeUnload;


这篇关于单击“提交"按钮时,警告用户有关未保存的数据脚本不会停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆