显示本地存储中的数组值
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() {});
如果有人能提供帮助,将不胜感激。
我做了一个 jsfiddle,它似乎在那里工作:jsfiddle
尝试:
localStorage.getItem('player1Favourite');
或:
localStorage.player1Favourite
您可能想看看:
this topic
或
Mozilla
我不确定我是否理解正确,因为如果您只想检索值,您可以使用相同的代码,只需从代码中删除插入并更改 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.
}
});
我正在尝试显示已存储在本地存储中的数组中的值。我已经能够将这些值存储在本地存储中,并通过单击按钮添加其他值,但我无法取回这些值并将它们显示在页面上。
这是我将值添加到本地存储的代码。
$("#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() {});
如果有人能提供帮助,将不胜感激。
我做了一个 jsfiddle,它似乎在那里工作:jsfiddle
尝试:
localStorage.getItem('player1Favourite');
或:
localStorage.player1Favourite
您可能想看看: this topic 或 Mozilla
我不确定我是否理解正确,因为如果您只想检索值,您可以使用相同的代码,只需从代码中删除插入并更改 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.
}
});