我有一个从用户选择的7个值中绘制图形的函数。它工作得很好,但为每个变量设置的本地存储值在刷新时会消失
function drawChart() {
var dates = document.getElementById("datepicker").value;
var days;
for(days=1;days<=dates;days++)
{
document.write(days);
}
const coordinates2 = dates;
const[one,two,three,four,five,six,seven
]= coordinates2.split(',')
localStorage.setItem('one',one); // storing is successful but clears on refresh
// i have also tired Json.stringify but it did not good
localStorage.setItem('one', JSON.stringify(one)); //storing is successful but clears on refresh
}我可以看到浏览器正在存储变量值,一旦浏览器刷新,它就会消失。还有什么我需要做的吗。非常感谢您的宝贵时间。
发布于 2021-04-27 10:26:39
在设置该值之前,您需要检查该值是否已设置
if (!localStorage.getItem('one')){
localStorage.setItem('one',one);
}https://stackoverflow.com/questions/67276121
复制相似问题