简体   繁体   English

如何记住尚未提交的表单数据?

[英]How to remember form data that has not been submitted?

How can you make the browser remember what the user typed in the form, which has not yet been submitted and make the page refreshing not affect the data entered? 如何让浏览器记住用户在表单中键入的内容(尚未提交),并刷新页面而不影响输入的数据?

I have a form in which the user enters a number. 我有一个用户输入数字的表格。 Initially the form has 0 by default. 最初,该表单默认为0。 I am storing the data in localStorage, so the browser can remember the data. 我将数据存储在localStorage中,因此浏览器可以记住数据。 However, when the page is refreshed, the user-entered data disappears and 0 is displayed by default. 但是,刷新页面时,默认情况下,用户输入的数据消失并且显示为0。 (still the localStorage data exists for it) (仍然存在它的localStorage数据)

I tried to use jQuery's 我试图使用jQuery的

$(".formClassName").val(localStorage.getItem(key)); 

but it does not work. 但它不起作用。 Can anyone give me a piece of advice on this?Thank you in advance. 任何人都可以给我一些建议吗?谢谢。

Edited: My form looks like this: 编辑:我的表单看起来像这样:

<form>
  <!--There are multiple forms, and the only difference among them is the "name" attribute -->
  Enter a number <input type="text" value="0" class"dataEntered" name="****">
  <!--The button below saves the data entered in the above form -->
  <input type="button" class="savedata" value="Save Value" name="****">
</form>

And I am adding the data to localStorage like below: 我将数据添加到localStorage,如下所示:

//JavaScript
<script>
//Using on because the website retrieves the above form dynamically
$(document).on("click", ".saveData", function(e){
    //retrieve the number entered in the form
    var userNum = $(this).siblings(".dataEntered").val();
    //retrieve the value in name attribute
    var thisFormName = $(this).attr("name");
    //store the data
    localStorage.setItem(thisFormName, userNum);

    //Now that the save button has been pressed (not submitted to the 
    //server yet), and the data is stored in localStorage, I want to
    //the page to show the number in userNum even after you refresh the page
    //but this does not work.
    $(".dataEntered").val(localStorage.setItem(thisFormName));

});
</script>

Checkout this codepen I have it shows a functional solution to the problem. 签出这个Codepen我有它显示了该问题的功能解决方案。 Also you need to make sure jQuery script checks if the DOM is ready, you can do that by using $(function() { }) a short hand for .ready() . 另外,您还需要确保jQuery脚本检查DOM是否已准备就绪,您可以通过使用.ready()的简写$(function() { })实现。

$(function() {
  var input = $("[type=text]");
  var thisFormName = input.attr("name");

  if (localStorage.getItem(thisFormName)) {
    var value = parseInt(localStorage.getItem(thisFormName));
    input.val(value);
  } 

  $(document).on("click", ".savedata", function(e) {
    var userNum = input.val();
    localStorage.setItem(thisFormName, userNum);
    input.val(localStorage.getItem(thisFormName));
  });
});

use cookie: 使用Cookie:

function addCookie(sName,sValue,day) {
    var expireDate = new Date();
    expireDate.setDate(expireDate.getDate()+day);
    document.cookie = escape(sName) + '=' + escape(sValue) +';expires=' + expireDate.toGMTString(); 
}
function getCookies() {
    var showAllCookie = '';
    if(!document.cookie == ''){
    var arrCookie = document.cookie.split('; ');
    var arrLength = arrCookie.length;
    var targetcookie ={};
   for(var i=0; i<arrLength; i++) {
        targetcookie[unescape(arrCookie[i].split('=')[0])]= unescape(arrCookie[i].split('=')[1]);
        }
    return targetcookie;
}

addCookie('type','1',1024);
var cookiesample = getCookies();
$(".formClassName").val(cookiesample.type); 

cookiesample.type could be remembered unless the cookie is deleted. 除非删除cookie,否则可以记住cookiesample.type。

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

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