假設您不想設置服務器來存儲此信息,則只剩下幾個選項。具體來說就是客戶端存儲。我會覆蓋localStorage
,因爲它是最容易使用的,你已經在上面提到過了。
這是localStorage API。如果您認爲界面過於複雜,我會建議給它一個通讀,也許還有一個tutorial(我個人認爲Mozilla文檔更好,但這個tut看起來更適合您的應用程序)。
總之,您將使用setItem(...)
和getItem(...)
方法。
localStorage.setItem("My test key", "My test value");
console.log(localStorage.getItem("My test key")); // --> My test value
因此,對於您的應用程序,我建議創建一個對象,以您的值存儲在再附加說對象localStorage
。
var state = {}
// Get values from your form however you feel fit
values = getValues(...);
function setValues(values) {
// Do this for all the values you want to save
state.values = values
// Continues for other values you want to set...
}
// Save values to localStorage
localStorage.setItem("Application_State", state);
現在用於檢索用戶返回您的網站時的狀態。
// Application_State is null if we have not set the state before
var state = localStorage.getItem("Application_State") || {};
function updateValues() {
values = state.values;
// Render your values to the UI however you see fit
}
這是一個非常簡要介紹在這裏,我會鼓勵你把它發佈到生產就緒的網站之前,請仔細閱讀這些東西的文檔。
希望這會有所幫助& &祝你好運!
你應該有一個服務器端的API POST數據並將其存儲在您的數據庫 –
對不起,先生,我不知道該怎麼做..我從其他線程學習我可以使用localstorage,但它也是複雜。 –