2016-04-28 32 views
0

我確實設法將多個數據保存到localStorage中,知道我想從localStorage調用所有數據到表單。下面是代碼,將其保存到本地存儲如何從localstorage獲取所有數據以將其顯示到表單中?

功能保存() {

// 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)); 

}

+0

確實[這個答案]獲取值(http://stackoverflow.com/questions/8419354/get-html5-localstorage-keys)的幫助?頂部的投票答案還顯示瞭如何獲得與該鍵相對應的值 – Tung

回答

0

你可以用這個

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; 
相關問題