提示保存数据,如果和已更改时 [英] Prompt to save data if and when changes have been made

查看:212
本文介绍了提示保存数据,如果和已更改时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用asp.net和我需要显示一个提示用户是否更改了该网页,他们不小心关闭了浏览器。

I am using asp.net and I need to display a prompt to the user if they have made changes to the web page, and they accidentally close down the browser.

页可能是从编辑个人资料任何一个提交索赔等。

The page could be anything from "Edit Profile" to a "Submit a Claim" etc.

如何可以显示消息框,确保其只显示如果更改了(而不是,更改用户,那么撤消 - 荷兰国际集团的变化,并关闭浏览器)

How can I can display the messagebox, ensuring that it is displayed only if changes have been made (as opposed to, the user making changes, then undo-ing the changes, and shutting down the browser)

推荐答案

我在过去做的是使用一些客户端脚本这onbeforeunload事件过程中执行此检查....

What I have done in the past is use some client side scripting which does this check during the onbeforeunload event....

var showPrompt=true;
var isDirty=false;
var hidePopup=false;

    function onBeforeUnload() {
        if (showPrompt) {
            if (isDirty) {
                if (hidePopup || confirm("Click ok to save your changes or click cancel to discard your changes.")) {
                    doSave();
                }
            }
        }
        showPrompt = true;
        hidePopup = false;
    }

  • ShowPrompt可以设置为false,当你点击一个锚标记,不会浏览您离开页面。例如<一的onclick ='showPrompt =假HREF =JavaScript的:DOX()'/>
  • isDirty可以用来跟踪,当你需要保存的东西。例如,你可以做这样的事情 $(输入)的onchange(函数(){isDirty = TRUE;}); 来跟踪撤消的可能要替换isDirty一个函数,它会检查当前状态从上次保存的状态。
  • HidePopup让我们强制保存不确认的用户。
    • ShowPrompt can be set to false when your clicking on an anchor tag which won't navigate you away from the page. For example <a onclick='showPrompt=false' href='javascript:doX()'/>
    • isDirty can be used to track when you need to save something. You could for example do something like $("input").onchange(function(){isDirty=true;}); To track undo's you might want to replace isDirty with a function which checks the current state from the last saved state.
    • HidePopup lets us force a save without confirming to the user.
    • 这篇关于提示保存数据,如果和已更改时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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