如何保存到本地存储

时间:2022-07-11 16:51:28

So I have made a table table elements and functions for the pop up and the form. Appending element on clicking save button also works. However I can't make it work the content to be stored and pulled from local storage in refresh page. I am somehow trying to populate the cell with currently generated ID . Considering the fact that I me new at JavaScript I am totally missing something Can someone give me idea what is that. The Code

所以我为弹出窗口和表单创建了一个表格元素和函数。单击“保存”按钮时添加元素也可以。但是,我无法使其在刷新页面中存储和从本地存储中提取的内容工作。我试图用当前生成的ID填充单元格。考虑到我是JavaScript的新手,我完全错过了某些东西。有人可以告诉我这是什么。代码

 /*save to td */
 $('#save').click(function () {
    localStorage.setItem(clickID, JSON.stringify(clickID));
    var theName = $('input[name=name]').val();
    var theLastName = $('input[name=lastname]').val();
    $('input[name=name]').val("");
    $('input[name=lastname]').val("");
    var $fullCell = $('<p>' + theName + '' + theLastName +     '</p>');
    if((theLastName+theLastName).length > 0){
       $(callingID).append($fullCell);
       $(callingID).css('background-color', 'yellow');
    }
});

https://jsfiddle.net/9zcj3ab8/27/

1 个解决方案

#1


0  

In javascript keys for an object must be strings. You can think of localStorage as a persistent Javascript object. Since you are calling JSON.stringify on clickID I am assuming clickID is a javascript object.

在javascript键中,对象必须是字符串。您可以将localStorage视为持久性Javascript对象。由于您在clickID上调用JSON.stringify,我假设clickID是一个javascript对象。

The first argument to setItem must be a string, followed by any valid js type for the second argument (documentation for setItem here: https://developer.mozilla.org/en-US/docs/Web/API/Storage/setItem)

setItem的第一个参数必须是一个字符串,后跟第二个参数的任何有效js类型(这里的setItem文档:https://developer.mozilla.org/en-US/docs/Web/API/Storage/setItem)

If clickID is a string:

如果clickID是一个字符串:

Additionally, you are saving to localStorage just clickID and a stringified version of clickID. It seems to me that the intended behavior is to store to localStorage a string called clickID as a key (first argument) with the first name and last name as the value (second argument).

此外,您只需将clickID和clickID的字符串化版本保存到localStorage即可。在我看来,预期的行为是将一个名为clickID的字符串存储到localStorage作为键(第一个参数),其名称和姓氏为值(第二个参数)。

Not sure if that is the wrong jsfiddle but your $('#save').click function is nowhere to be found there.

不确定这是不是错误的jsfiddle而是$('#save')。点击功能无处可寻。

#1


0  

In javascript keys for an object must be strings. You can think of localStorage as a persistent Javascript object. Since you are calling JSON.stringify on clickID I am assuming clickID is a javascript object.

在javascript键中,对象必须是字符串。您可以将localStorage视为持久性Javascript对象。由于您在clickID上调用JSON.stringify,我假设clickID是一个javascript对象。

The first argument to setItem must be a string, followed by any valid js type for the second argument (documentation for setItem here: https://developer.mozilla.org/en-US/docs/Web/API/Storage/setItem)

setItem的第一个参数必须是一个字符串,后跟第二个参数的任何有效js类型(这里的setItem文档:https://developer.mozilla.org/en-US/docs/Web/API/Storage/setItem)

If clickID is a string:

如果clickID是一个字符串:

Additionally, you are saving to localStorage just clickID and a stringified version of clickID. It seems to me that the intended behavior is to store to localStorage a string called clickID as a key (first argument) with the first name and last name as the value (second argument).

此外,您只需将clickID和clickID的字符串化版本保存到localStorage即可。在我看来,预期的行为是将一个名为clickID的字符串存储到localStorage作为键(第一个参数),其名称和姓氏为值(第二个参数)。

Not sure if that is the wrong jsfiddle but your $('#save').click function is nowhere to be found there.

不确定这是不是错误的jsfiddle而是$('#save')。点击功能无处可寻。