假设您不想设置服务器来存储此信息,那么您在这里只剩下几个选项。具体来说,客户端存储。我将介绍localStorage,因为它是最容易使用的,而且您在上面已经提到过。
这是localStorage API。如果您认为界面过于复杂,我建议您通读一遍,也许还可以加上tutorial(我个人认为 Mozilla Docs 更好,但这个 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
}
这是一个非常简短的介绍,我建议您在将其发布到可生产的网站之前仔细阅读有关这些内容的文档。
希望这会有所帮助,祝你好运!