Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 显示本地存储中的数组值_Javascript_Jquery_Arrays_Local Storage - Fatal编程技术网

Javascript 显示本地存储中的数组值

Javascript 显示本地存储中的数组值,javascript,jquery,arrays,local-storage,Javascript,Jquery,Arrays,Local Storage,我试图显示已存储在本地存储中的数组中的值。我可以将这些值存储在本地存储器中,并通过单击按钮添加其他值,但我无法检索这些值并将其显示在页面上 这是我为本地存储添加值的代码 $("#player1Link").click(function() { if (localStorage.getItem("player1Favourite") !== null) { var a = JSON.parse(localStorage.getItem('p

我试图显示已存储在本地存储中的数组中的值。我可以将这些值存储在本地存储器中,并通过单击按钮添加其他值,但我无法检索这些值并将其显示在页面上

这是我为本地存储添加值的代码

$("#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”)。单击(函数(){})

如果有人能帮忙,我将不胜感激。

我制作了一个JSFIDLE,它似乎在那里工作:

尝试:

或:

您可能想看看: 或

我不确定是否正确理解您的意思,因为如果您只想检索值,可以使用相同的代码,只需从代码中删除插入内容并更改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.
    }
});
localStorage.player1Favourite
$("#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.
    }
});