[英]Local Storage resetting the value after page refresh
I want to input data from user input and store to local storage, but when the page get refresh the value of local storage reset.我想从用户输入中输入数据并存储到本地存储,但是当页面刷新时本地存储的值会重置。 How to store all user input in local storage even I hit refresh?
即使我点击刷新,如何将所有用户输入存储在本地存储中?
JavaScript. JavaScript。
var myArr = [];
function botResponse(rawText) {
var inputText = rawText; //user input
myArr.push(inputText);
var arrString = myArr.join(", ");
var existing = localStorage.getItem('UserInput');
existing = [];
existing.push(arrString);
localStorage.setItem('UserInput',existing);
// var arrStringTwo = existing.join(", ");
// Bot Response
var new_value = window.localStorage.getItem("UserInput");
----------
}
You're resetting it with existing = [];
您正在使用
existing = [];
重置它. .
You can't store arrays directly in localStorage
, it can only store strings.您不能直接在
localStorage
存储数组,它只能存储字符串。 You need to convert to and from JSON.您需要在 JSON 之间进行转换。
var existing = localStorage.getItem('UserInput');
if (existing) {
existing = JSON.parse(existing);
} else {
existing = [];
existing.push(arrString);
localStorage.setItem('UserInput',JSON.stringify(existing));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.