简体   繁体   English

如何保存到本地存储

[英]How to save to local storage

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 . 我以某种方式试图用当前生成的ID填充单元格。 Considering the fact that I me new at JavaScript I am totally missing something Can someone give me idea what is that. 考虑到我刚接触JavaScript的事实,我完全错过了一些东西,有人可以告诉我那是什么吗? The Code 编码

 /*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/ https://jsfiddle.net/9zcj3ab8/27/

In javascript keys for an object must be strings. 在javascript中,对象的键必须是字符串。 You can think of localStorage as a persistent Javascript object. 您可以将localStorage视为持久性Javascript对象。 Since you are calling JSON.stringify on clickID I am assuming clickID is a javascript object. 由于您在clickID上调用JSON.stringify, clickID我假设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/zh-CN/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. 此外,您仅将clickID和stringID的字符串化版本保存到localStorage。 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的字符串作为键(第一个参数)存储到localStorage,并且将名字和姓氏作为值(第二个参数)。

Not sure if that is the wrong jsfiddle but your $('#save').click function is nowhere to be found there. 不知道这是否是错误的jsfiddle,但是在那里找不到$('#save').click函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM