[英]How to display LocalStorage data in html view page on refresh using javascript or jQuery?
From the below code, I can able to set or store
my entered data in localStorage
, but I am not sure how can I retrieve and display the same data in the view page if I do refreshing the page. 从下面的代码中,我可以在localStorage
set or store
我输入的数据,但是如果我刷新页面,我不知道如何在视图页面中检索和显示相同的数据。 I have tried with localStorage.getItem()
. 我尝试过localStorage.getItem()
。 The entered data is showing on clicking of Save
button each time, that's fine and perfect. 每次单击“ Save
按钮时显示输入的数据,这很好,很完美。 But if I refresh the page then it is not showing anything, it is resetting, I want to show/display the same eariler Saved data
even if I refresh the page also on my view. 但是,如果我刷新页面然后它没有显示任何内容,它正在重置,我想show/display the same eariler Saved data
即使我也在我的视图上刷新页面。 Please let me know how to do this ? 请告诉我怎么做? Thanks in advance ! 提前致谢 !
html: 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">
×
</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: 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);
}
});
});
Final solution: 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);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.