简体   繁体   English

将表格内容存储到HTML5本地存储中

[英]Storing table content to HTML5 local storage

So I have multiple input boxes in HTML. 因此,我在HTML中有多个输入框。 Then I create a table with jquery and on a click of a button I store values from the input boxes into my table. 然后,我使用jquery创建一个表,然后单击按钮,将输入框中的值存储到表中。 What I don't know how to do is how to store those values into local storage and the when the page reloads show them in the table. 我不知道该怎么做是如何将这些值存储到本地存储中,以及何时页面重新加载在表中显示它们。

This is my code for adding a new line to the table: 这是我在表中添加新行的代码:

    $().ready(function(){
      $("#addLine").click(function(){
        var chore = $("#chore").val();
        var type= $("#type").val();
        var importance = $("#importance").val();
        counter++;
        var newLine = "<tr><td>"+counter+"</td><td>"+chore+"</td>   
        <td>"+type+"</td><td>"+importance+"</td><td>"+date+"</td></tr>";
        $("#tabela tbody").append(newLine);
});
});

Use setItem method of localStorage : 使用localStorage的 setItem方法:

localStorage.setItem( "tabledata", $("#tabela").html() );

Later you can fetch it like this: 稍后您可以像这样获取它:

var tabledata  = localStorage.getItem( "tabledata" );
$("#tabela").html( tabledata ); 

You have to make a array 你必须做一个数组

var tableItems = [];
//getting old items
tableItems = localStorage.getItem("tabledata");

//push item on new add
tableItems.push({
"chore":"",
"type":"",
"importance":"",
"date":""
});

You have to store it in 您必须将其存储在

localStorage.setItem( "tabledata", tableItems ) );

Generate <table> from this JSON stored in localStorage 从存储在localStorage JSON生成<table>

to set a item in browser local storage: 在浏览器本地存储中设置项目:

localStorage.setItem("key", value);

to get a item from browser local storage: 从浏览器本地存储中获取项目:

var value = localStorage.getItem("key");

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

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