[英]What is the best way to get the data from an array that is stored in localstorage?
Below as you can see in my code i created a input field for users that i want to store inside of an array, which also i want to store that array into localstorage.下面正如您在我的代码中看到的,我为我想存储在数组中的用户创建了一个输入字段,我也想将该数组存储到 localstorage 中。 Now i think that stored the values that i give trough the input field in localsorage , beacuse that i can see from my browser application in chrome.
现在我认为存储了我通过 localsorage 中的输入字段给出的值,因为我可以从我的 chrome 浏览器应用程序中看到。 Now with this code i have some problem.
现在有了这个代码,我有一些问题。
Each value that i add one by one as you can see in the picture below ,whenever i refresh the browser , the values disapear from the browser?如下图所示,我逐一添加的每个值,每当我刷新浏览器时,这些值都会从浏览器中消失吗? So if someone can help me out with my code i would appriciate a lot , and also give me some advice , since im very new to localstorage and cookies and i am trying to learn
因此,如果有人可以帮助我解决我的代码,我会非常感谢,并给我一些建议,因为我对 localstorage 和 cookie 非常陌生,而且我正在努力学习
<input type="text" id="username"><br><br>
<input type="submit" id="btn">
<p id="demo"></p>
<script>
var btn=document.getElementById("btn");
var user=document.getElementById("username");
var names=[];
btn.onclick=function(){
names.push(user.value);
localStorage.setItem("names",JSON.stringify(names));
var f =JSON.parse(localStorage.getItem("names"));
var x;
var i;
for(i=0;i<f.length;i++){
x+=f[i]+"<br>";
}
document.getElementById("demo").innerHTML=x;
}
</script>
You're getting undefined
because you never assign a value to x
prior to using it in x+=f[i]+"<br>"
, so it gets the default value undefined
, which is then converted to string.您得到
undefined
是因为您在x+=f[i]+"<br>"
使用它之前从未将值分配给x
,因此它获得默认值undefined
,然后将其转换为字符串。 Assign ""
to x
before the loop.在循环之前将
""
分配给x
。
Side note: There's no need for f
in that code at all.旁注:该代码中根本不需要
f
。 Just use names
:只需使用
names
:
localStorage.setItem("names",JSON.stringify(names));
var x = "";
for (var i = 0; i < names.length; ++i) {
x += names[i] + "<br>";
}
You might also consider using join
instead of a loop:您也可以考虑使用
join
而不是循环:
localStorage.setItem("names",JSON.stringify(names));
var x = names.join("<br>");
Beware that if any name is entered that contains a <
or &
, it could potentially mess up your output, since of course those characters start things that are special in HTML (tags and character references, respectively).请注意,如果输入的任何名称包含
<
或&
,则可能会弄乱您的输出,因为当然这些字符开始于 HTML 中的特殊内容(分别是标签和字符引用)。 To prevent that, you might use map
before join
:为了防止这种情况,您可以在
join
之前使用map
:
localStorage.setItem("names",JSON.stringify(names));
var x = names.map(function(name) {
return name.replace(/&/g, "&").replace(/</g, "<");
}).join("<br>");
or in modern JavaScript:或在现代 JavaScript 中:
localStorage.setItem("names",JSON.stringify(names));
const x = names.map(name => name.replace(/&/g, "&").replace(/</g, "<"))
.join("<br>");
That's only valid if you're outputting to an element's body, as you are in that code;这仅在您输出到元素主体时才有效,就像您在该代码中一样; if you were outputting to an attribute value, it would be more complicated.
如果你输出到一个属性值,它会更复杂。 (Some folks would also change
>
to >
, but there isn't any need to if, again, you're outputting to the body of an element and not within a tag.) (有些人也会将
>
更改为>
,但如果再次输出到元素的主体而不是标签内,则没有任何必要。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.