admin管理员组文章数量:1399839
I'm about to code, in Javascript some code (involving looping on each <input>
and adding listeners):
- allowing, after keypress, to save all
<input>
values tolocalStorage
- restore all
<input>
values fromlocalStorage
in the case the page/browser has been closed and reopened on the same page
But maybe is there an automatic way, provided by the browsers?
e.g. by adding an attribute to <input>
, similar to <input autofocus>
(which is not related here)
Question: is there an autosave feature of <form>
<input>
HTML tags?
I'm about to code, in Javascript some code (involving looping on each <input>
and adding listeners):
- allowing, after keypress, to save all
<input>
values tolocalStorage
- restore all
<input>
values fromlocalStorage
in the case the page/browser has been closed and reopened on the same page
But maybe is there an automatic way, provided by the browsers?
e.g. by adding an attribute to <input>
, similar to <input autofocus>
(which is not related here)
Question: is there an autosave feature of <form>
<input>
HTML tags?
3 Answers
Reset to default 5As far as I know, there is no built-in way to do that, you should do it manually;
function persist(thisArg) {
localStorage.setItem(thisArg.id, thisArg.value);
}
<input id="test" onchange="persist(this)" />
persist and retrieve all together:
function persist(event) {
localStorage.setItem(event.target.id, event.target.value);
}
// you may use a more specific selector;
document.querySelectorAll("input").forEach((inputEl) => {
inputEl.value = localStorage.getItem(inputEl.id);
inputEl.addEventListener("change", persist);
});
<input id="test" />
there is no automatic way to do that. you have two options :
- save the data by code
example:
localStorage.setItem('testObject', JSON.stringify(yourObject)); // for storing data
JSON.parse(localStorage.getItem('yourObject')); // for retrieving data
code snippet:
// for saving data
function saveData(el) {
localStorage.setItem(el.id, JSON.stringify(el.value));
}
// for retrieving data on page load
function getData() {
var inp = document.getElementById("inp");
inp.value = JSON.parse(localStorage.getItem('inp')) || "";
}
<body onload="getData()">
<input id="inp" onchange="saveData(this)" />
</body>
- try a helper library like persisto
Based on the accepted answer, here is a one-liner that can be useful:
document.querySelectorAll('input:not([type="submit"])').forEach(elt => { elt.value = localStorage.getItem(elt.name); elt.addEventListener("change", e => { localStorage.setItem(e.target.name, e.target.value); }); });
It serializes/deserializes the <input>
s to localStorage
, indexed by their attributes name
.
本文标签: javascriptAutosave all inputs value to localStorage and restore them on page reloadStack Overflow
版权声明:本文标题:javascript - Auto-save all inputs value to localStorage and restore them on page reload - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744151115a2593060.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论