简体   繁体   English

如何通过使用本地存储来记住输入值?

[英]how to remember inputs value by using local storage?

I have tried to use local storage to remember the input's value after refreshing page. 我尝试使用本地存储来刷新页面后记住输入的值。 but my code does not work. 但是我的代码不起作用。

here is the HTML code 这是HTML代码

<input type="text" name="name" onkeyup="saveValue(event)"/> 
<input type="text" name="name" onkeyup="saveValue(event)"/> 
<input type="text" name="age" onkeyup="saveValue(event)"/> 

and here is javascript 这是javascript

<script type="text/javascript">
var nameArr = ["name"];
var inputs = document.getElementsByName('name');
inputs.forEach(function(el){
    el.value = getSavedValue(el);  
})

function saveValue(e) {
  var name = e.target.name;
  var val = e.target.value;
  localStorage.setItem(name, val);
}

function getSavedValue(v) {
  if (!localStorage.getItem(v)) {
    return "";
  }
  return localStorage.getItem(v);
}
</script>

if there is a way to solve this problem please tell me. 如果有办法解决这个问题,请告诉我。 and if there is a way to do that with jquery I will be thankful to tell me that. 如果有办法用jquery做到这一点,我很乐意告诉我。

On your code you are passing the input object as a parameter instead of its name (or value; you choose). 在代码上,您将输入对象作为参数而不是其名称(或值;您选择)传递。 As localStorage only stores String key-value pairs, it won't work as you're trying to find a key that is an object. 由于localStorage仅存储字符串键值对,因此在您尝试查找作为对象的键时将无法使用。

in the forEach instead of: 在forEach中,而不是:

el.value = getSavedValue(el);

set: 组:

el.value = getSavedValue(el.name);

or let the "getSavedValue" function accept an object as parameter, but to access localStorage you must pass a string as the key. 或者让“ getSavedValue”函数接受一个对象作为参数,但是要访问localStorage,必须传递一个字符串作为键。

Here are couple of things. 这是几件事情。 First instead of onkeyup use onblur so value will be saved in storage only when the focus is removed from the element. 首先,使用onblur代替onkeyup以便仅在将焦点从元素移开时将值保存在存储中。

Secondly use a common class inputs in this case and give separate name to each element. 其次,在这种情况下,使用通用的类inputs ,并为每个元素分别name

Then get all the elements with same class, iterate through it and get value of name property using getAttribute . 然后获取具有相同类的所有元素,对其进行迭代,并使用getAttribute获得name属性的值。 Use this value to check if there exist a key in localStorage 使用此值检查localStorage是否存在密钥

 var nameArr = ["name"]; var inputs = [...document.getElementsByClassName('inputs')]; inputs.forEach(function(el) { console.log() el.value = getSavedValue(el.getAttribute('name')); }) function saveValue(e) { var name = e.target.name; var val = e.target.value; localStorage.setItem(name, val); } function getSavedValue(v) { if (!localStorage.getItem(v)) { return ""; } return localStorage.getItem(v); } 
 <input type="text" class='inputs' name="firstName" onblur="saveValue(event)" /> <input type="text" class='inputs' name="lastName" onblur="saveValue(event)" /> <input type="text" class='inputs' name="age" onblur="saveValue(event)" /> 

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

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