【发布时间】:2021-06-22 10:33:15
【问题描述】:
我一直在尝试寻找一种无需刷新即可查看来自 localStorage 的新数据和更新数据的方法。我目前有一个文本框,旁边有一个提交按钮。在文本框中输入内容并单击“提交”后,您输入的文本将保存到 localStorage。然后我将它显示在文本框下方。所有这些都可以正常工作,但唯一的问题是 localStorage 中的数据不会立即显示,您必须重新加载页面。我希望数据不断更新,这样您就不必每次想要查看来自 localStorage 的数据时都重新加载页面。这是我目前所拥有的:
//"saves" is the id I assigned to the div that stores the saves.
document.getElementById('saves').innerHTML = localStorage.getItem('item-array-HTML-program');
//save() gets called when the submit button is clicked.
function save() {
//data is an empty array, and the variable "var" is the textboxes.value
var data = [], val = document.getElementById('textbox').value;
//the data that's already been stored in the HTML program key gets pushed into the empty array
"data"
data.push(localStorage.getItem('item-array-HTML-program'));
//the text in the text box gets pushed into the array "data"
data.push(val);
//the array "data" gets set into localStorage.
localStorage.setItem('item-array-HTML-program', data);
}
【问题讨论】:
-
这很混乱,请添加HTML Body部分!
-
您可以在您使用的任何输入上绑定
onChange处理程序,这将更改将调用save()的数据
标签: javascript