Forms 在HTML编辑表单上,有什么好方法可以重置和记住发布的值功能?

Forms 在HTML编辑表单上,有什么好方法可以重置和记住发布的值功能?,forms,Forms,我有一个既有服务器端验证又有客户端验证的表单。 它是一个编辑表单,因此原始用户值最初是预先填充的 e.g. The original pre-populated values are: username = yeo surname = yang phonenumber = 11-12345 现在,用户编辑以下内容并提交 e.g. The edited submitted values are: username = yeoNew surname = yangNew phonenumber =

我有一个既有服务器端验证又有客户端验证的表单。 它是一个编辑表单,因此原始用户值最初是预先填充的

e.g. The original pre-populated values are:
username = yeo
surname = yang
phonenumber = 11-12345
现在,用户编辑以下内容并提交

e.g. The edited submitted values are:
username = yeoNew
surname = yangNew
phonenumber = 12-1111
这将被提交到服务器端,并且服务器端验证失败,因为不允许以12开头的电话号码

无论如何,表单会显示回用户

e.g. The redisplayed form values are:
username = yeoNew
surname = yangNew
phonenumber = 12-1111
这是因为我的表单允许用户记住他们提交的值

在这个阶段,我希望允许用户能够使用客户端javascript将表单值重置为原始值。这类似于重置功能

e.g. The reset button will restore the form values to:
username = yeo
surname = yang
phonenumber = 11-12345
此重置功能的原因是,我希望用户可以选择从原始值再次编辑电话号码

我的问题是: 什么是跟踪HTML中原始值的好方法,这样我就可以用javascript恢复它

我想在表单元素中有一个名为orig=''的新属性,它将存储这个值。 这是个好主意吗? 还有其他方法吗


谢谢

我会使用HTML5本地存储。 看

使用jquery,我会这样做:

   <script type="text/javascript">
      function load() {
        if (localStorage["username"]) {
            $('#username').val(localStorage["username"]);
        }
        if (localStorage["surname"]) {
            $('#surname').val(localStorage["surname"]);
        }
        if (localStorage["phone"]) {
            $('#phone').val(localStorage["phone"]);
        }
      }

      function save() {
        localStorage["username"] =  $('#username ').val();
        localStorage["surname"] =  $('#surname').val();
        localStorage["phone"] =  $('#phone').val();
      }
   </script>

函数加载(){
如果(本地存储[“用户名”]){
$('#username').val(localStorage[“username]”);
}
if(本地存储[“姓氏]){
$('#姓氏').val(localStorage[“姓氏]);
}
如果(本地存储[“电话”]){
$('#phone').val(localStorage[“phone”]);
}
}
函数save(){
localStorage[“username”]=$('#username').val();
localStorage[“姓氏]=$(“#姓氏”).val();
localStorage[“phone”]=$('#phone').val();
}

Hi,这是一个有趣的新解决方案。我已决定坚持我原来的想法。然而,你的解决方案值得深思。干杯