从下面的代码中,我可以在
html:
localStorage
中设置或存储输入的数据,但是如果我刷新页面,我不确定如何检索和显示相同的数据在视图页面上。 我已经尝试使用localStorage.getItem()
。每次点击Save
按钮时都会显示输入的数据,这很好,也很完美。 但是,如果我刷新页面,它就不会显示任何东西,而是重置,我想在我的视图上显示/显示相同的先前保存的数据
,即使我刷新页面。 请告诉我如何做到这一点? 提前致谢!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:
$(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 availale.