gpt4 book ai didi

javascript - 如何在使用 javascript 或 jQuery 刷新时在 html View 页面中显示 LocalStorage 数据?

转载 作者:可可西里 更新时间:2023-11-01 13:44:40 25 4
gpt4 key购买 nike

从下面的代码中,我可以在 localStorage设置或存储我输入的数据,但我不确定如何在 localStorage 中检索和显示相同的数据如果我刷新页面,则查看页面。我已经尝试使用 localStorage.getItem()。每次单击Save 按钮时都会显示输入的数据,这非常完美。但是如果我刷新页面然后它不显示任何东西,它正在重置,我想 show/display the same eariler Saved data 即使我也在我的 View 中刷新页面。请让我知道该怎么做?提前致谢!

html:

<button class = "btn btn-primary" data-toggle = "modal" data-target = "#myModal">
Click
</button>

<div class = "modal fade" id = "myModal" tabindex = "-1" role = "dialog"
aria-labelledby = "myModalLabel" aria-hidden = "true">

<div class = "modal-dialog">
<div class = "modal-content">
<div class = "modal-header">
<button type = "button" class = "close" data-dismiss = "modal" aria-hidden = "true">
&times;
</button>
</div>
<div class = "modal-body">
<textarea id="content"></textarea>
<br /><br/>
<button id="saveBtn">Save</button>
<div id="output" style="overflow: auto; height: 75px; width: 450px;">
</div>
</div>
<div class = "modal-footer">
<button type = "button" class = "btn btn-default" data-dismiss = "modal">
Close
</button>
</div>
</div>
</div>

</div>

js:

$(saveBtn).click(function(){
var noteContent = $(content).val();
var outputarea = $(output);
var info = [{ "content":"Doe" }];
$.each(info, function() {
i=0;
info[i].content = noteContent;
outputarea.append('<br>content:' + info[i].content + '<hr />');//it is appending the data to "outputarea" on clicking of Save button, but I want to show same data even if I refresh the page also(from localStorage) - how to do this
localStorage.setItem('info[i].content',info[i].content);
//localStorage.getItem(info[i].content);
i++;
function recallNote(){
$(content).val(info[i].content);
}
});

});

Fiddle有空。

最佳答案

最终解决方案:link

var info = [];

function allStorage(arr) {
keys = Object.keys(localStorage);
//console.log(keys);
i = keys.length;

while ( i-- ) {
arr.push( { content : localStorage.getItem(keys[i])} );
}
}

allStorage(info);
console.log(info);

var outputarea = $(output);

info.forEach((val)=>{
outputarea.append('<br>content:' + val.content + '<hr />');
})

$(saveBtn).click(function(){
var noteContent = $(content).val();


var obj = {content : noteContent};

info.push(obj);
console.log(info);
outputarea.append('<br>content:' + info[(info.length-1)].content + '<hr />');

localStorage.setItem('info['+(info.length-1)+'].content',info[(info.length-1)].content);


function recallNote(){
$(content).val(info[info.length-1].content);
}
});

关于javascript - 如何在使用 javascript 或 jQuery 刷新时在 html View 页面中显示 LocalStorage 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47102060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com