为了账号安全,请及时绑定邮箱和手机立即绑定

检测未保存的更改

检测未保存的更改

子衿沉夜 2019-11-06 10:43:23
我需要在ASP .Net应用程序中实现“未保存的更改”提示。如果用户修改了Web表单上的控件,并试图在保存之前导航离开,则将出现提示,警告他们尚未保存更改,并为他们提供取消并保留在当前页面上的选项。如果用户未触摸任何控件,则不应显示该提示。理想情况下,我想用JavaScript实现此功能,但是在我开始滚动自己的代码之前,是否有任何现有的框架或推荐的设计模式来实现这一目标?理想情况下,我希望可以以最小的更改轻松地在多个页面上重复使用的东西。
查看完整描述

3 回答

?
慕的地10843

TA贡献1785条经验 获得超8个赞

使用jQuery:


var _isDirty = false;

$("input[type='text']").change(function(){

  _isDirty = true;

});

// replicate for other input types and selects

根据需要与onunload/ onbeforeunload方法结合。


从注释中,以下内容引用了所有输入字段,而没有重复的代码:


$(':input').change(function () {

使用$(":input")是指所有输入,文本区域,选择和按钮元素。


查看完整回答
反对 回复 2019-11-06
?
天涯尽头无女友

TA贡献1831条经验 获得超9个赞

难题之一:


/**

 * Determines if a form is dirty by comparing the current value of each element

 * with its default value.

 *

 * @param {Form} form the form to be checked.

 * @return {Boolean} <code>true</code> if the form is dirty, <code>false</code>

 *                   otherwise.

 */

function formIsDirty(form) {

  for (var i = 0; i < form.elements.length; i++) {

    var element = form.elements[i];

    var type = element.type;

    if (type == "checkbox" || type == "radio") {

      if (element.checked != element.defaultChecked) {

        return true;

      }

    }

    else if (type == "hidden" || type == "password" ||

             type == "text" || type == "textarea") {

      if (element.value != element.defaultValue) {

        return true;

      }

    }

    else if (type == "select-one" || type == "select-multiple") {

      for (var j = 0; j < element.options.length; j++) {

        if (element.options[j].selected !=

            element.options[j].defaultSelected) {

          return true;

        }

      }

    }

  }

  return false;

}

还有一个:


window.onbeforeunload = function(e) {

  e = e || window.event;  

  if (formIsDirty(document.forms["someForm"])) {

    // For IE and Firefox

    if (e) {

      e.returnValue = "You have unsaved changes.";

    }

    // For Safari

    return "You have unsaved changes.";

  }

};

全部包起来,您会得到什么?


var confirmExitIfModified = (function() {

  function formIsDirty(form) {

    // ...as above

  }


  return function(form, message) {

    window.onbeforeunload = function(e) {

      e = e || window.event;

      if (formIsDirty(document.forms[form])) {

        // For IE and Firefox

        if (e) {

          e.returnValue = message;

        }

        // For Safari

        return message;

      }

    };

  };

})();


confirmExitIfModified("someForm", "You have unsaved changes.");

您可能还希望将beforeunload事件处理程序的注册更改为使用LIBRARY_OF_CHOICE的事件注册。


查看完整回答
反对 回复 2019-11-06
?
繁星淼淼

TA贡献1775条经验 获得超11个赞

在.aspx页中,您需要一个Javascript函数来判断表单信息是否为“脏”


<script language="javascript">

    var isDirty = false;


    function setDirty() {

        isDirty = true;

    }


    function checkSave() {

        var sSave;

        if (isDirty == true) {

            sSave = window.confirm("You have some changes that have not been saved. Click OK to save now or CANCEL to continue without saving.");

            if (sSave == true) {

                document.getElementById('__EVENTTARGET').value = 'btnSubmit';

                document.getElementById('__EVENTARGUMENT').value = 'Click';  

                window.document.formName.submit();

            } else {

                 return true;

            }

        }

    }

</script>

<body class="StandardBody" onunload="checkSave()">

然后在后面的代码中,将触发器添加到输入字段中,然后在提交/取消按钮上进行重置。


btnSubmit.Attributes.Add("onclick", "isDirty = 0;");

btnCancel.Attributes.Add("onclick", "isDirty = 0;");

txtName.Attributes.Add("onchange", "setDirty();");

txtAddress.Attributes.Add("onchange", "setDirty();");

//etc..


查看完整回答
反对 回复 2019-11-06
  • 3 回答
  • 0 关注
  • 580 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号