如何从localstorage获取所有数据以将其显示在表单中?

时间:2016-04-28 23:19:44

标签: arrays json local-storage

我确实设法将多个数据保存到localStorage,知道我想调用localStorage中的所有数据到表单。以下是将其保存到本地存储的代码

function save() {

// Parse any JSON previously stored in allEntries
var existingEntries = JSON.parse(localStorage.getItem("allEntries"));
if(existingEntries == null) existingEntries = [];
var date = document.getElementById("date").value;
var brakes = document.getElementById("brakes").value;
var chain = document.getElementById("chain").value;
var seats = document.getElementById("seats").value;
var handlebars = document.getElementById("handlebars").value;
var entry =
{
    "date": date,
    "brakes": brakes,
    "chain": chain,
    "seats": seats,
    "handlebars": handlebars
};
localStorage.setItem("entry", JSON.stringify(entry));
// Save allEntries back to local storage
existingEntries.push(entry);
localStorage.setItem("allEntries", JSON.stringify(existingEntries));

}

1 个答案:

答案 0 :(得分:1)

您可以使用此

获取值
var obj = JSON.parse(localStorage.getItem('entry'));

    document.getElementById("date").value = obj.date;
    document.getElementById("brakes").value = obj.brakes;
    document.getElementById("chain").value = obj.chain;
    document.getElementById("seats").value = obj.seats;
    document.getElementById("handlebars").value = obj.handlebars;