[英]Displaying Array Values From Local Storage
我试图显示已存储在本地存储中的数组中的值。 我已经能够将值存储在本地存储中,并在单击按钮时添加其他值,但我无法将值恢复并在页面上显示。
这是我的代码,它为本地存储添加了值。
$("#player1Link").click(function() {
if (localStorage.getItem("player1Favourite") !== null) {
var a = JSON.parse(localStorage.getItem('player1Favourite'));
if (a.indexOf(chosenPlayerId) === -1) {
a.push(chosenPlayerId);
localStorage.setItem('player1Favourite', JSON.stringify(a));
alert("Pushed in");
} else {
alert("Already in favourites");
}
} else {
var a = [];
a.push(chosenPlayerId);
localStorage.setItem('player1Favourite', JSON.stringify(a));
}
})
我希望能够单击一个按钮来检索值并在页面上显示它们,但我可以找出要在此功能中使用的代码。 $("#playerRetrieve").click(function() {});
如果有人可以提供帮助,我将不胜感激。
我不确定我是否理解正确,因为如果您只想检索值,可以使用相同的代码,只需从代码中删除插入并更改jQuery选择器即可。
$("#playerRetrieve").click(function() {
if (localStorage.getItem("player1Favourite") !== null) {
var a = JSON.parse(localStorage.getItem('player1Favourite'));
// Do what you want with the values, which are now in a.
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.