【问题标题】:How to display LocalStorage data in html view page on refresh using javascript or jQuery?如何在使用 javascript 或 jQuery 刷新时在 html 视图页面中显示 LocalStorage 数据?
【发布时间】:2017-11-04 03:42:30
【问题描述】:

从下面的代码,我可以set or store 我在localStorage 中输入的数据,但我不确定如果我刷新页面,如何在视图页面中检索和显示相同的数据。我试过localStorage.getItem()。输入的数据每次点击Save按钮时都会显示出来,很好很完美。但是,如果我刷新页面,那么它不会显示任何内容,它正在重置,我想show/display the same eariler Saved data 即使我也在我的视图中刷新页面。请让我知道该怎么做?提前致谢!

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 可用。

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    最终解决方案: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); 
        }
     });
    

    【讨论】:

    • 您能否让我知道或更新小提琴,我如何在我的小提琴中使用它。因为,如果我使用上面的,我仍然看不到。
    • 您编辑的答案没有给出任何内容?能否请您再检查一次,提前谢谢!
    • 刷新时我无法在查看页面上看到我保存的数据,我可以在控制台或开发人员工具中看到:console.log("info obj: "+JSON.stringify(info)); --->每次点击时都会附加保存的数据 - 很好,但如果我也刷新,我想在视图中显示这个保存的数据。(就像在保存按钮之后,我需要附加)。
    • 现在检查答案:)
    • 刷新页面后你想把localStorage放到这个textarea吗?
    猜你喜欢
    • 1970-01-01
    • 2017-06-20
    • 2012-02-20
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 2017-02-03
    • 1970-01-01
    • 2020-03-11
    相关资源
    最近更新 更多