[英]How to store multiple form data values to a single key in local storage
I have an html form when i submit the form the values are get stored in local storage but what i would like to do is whenever the form is submitted it should store the values in the same local storage but what happens is when i try to do the above approach it takes the last submitted value can anyone tell me how could i use the local storage to use the same key for multiple values. 当我提交表单时,我有一个html表单,将值存储在本地存储中,但我想要做的是每当提交表单时它应该将值存储在同一个本地存储中但是当我尝试执行时会发生什么上面的方法它需要最后提交的值,任何人都可以告诉我如何使用本地存储使用相同的键为多个值。
function addToCSVFile() {
// Collecting the names
var name = document.getElementById('firstName').value;
var middlename = document.getElementById('middleName').value;
var lastname = document.getElementById('lastName').value;
var data = {name: name, middlename: middlename, lastName: lastname};
console.log('Data is', data);
// alldata.push(data);
localStorage.setItem('Data', JSON.stringify(data));
clearData();
alert("Data Added Successfully", JSON.stringify(data));
}
Use an array and push new values to the array. 使用数组并将新值推送到数组。
The sequence would be something like: 序列将是这样的:
var array = JSON.parse(localStorage.getItem('Data') || '[]');
array.push(dataObject);
localStorage.setItem('Data', JSON.stringify(array));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.