假设您不想设置服务器来存储此信息,则只剩下几个选项。具体来说就是客户端存储。我会覆盖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,但它也是复杂。 –