简体   繁体   English

如何将多个表单数据值存储到本地存储中的单个键

[英]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.

相关问题 如何使用本地存储为单个键创建多个值 - How to create a multiple values for a single key using local storage 如何通过 POST API 调用存储具有单个键和多个值的表单数据 - How to store form data with single key and multiple values via POST API call 如何在本地存储中存储表单数据? - How to store form data in local storage? 将多个表单输入值以数组的形式存储在 JavaScript 中的本地存储中 - Store multiple form input values in local storage in JavaScript in the form of an array 如何向本地存储中的一个键添加多个值 - How to add multiple values to a key in local storage 如何在浏览器的本地存储中存储多个数据 - how to store multiple data in local storage of the browser HTML5本地存储存储选择框的数据,其中选择了多个值 - HTML5 Local Storage store form data for select box where multiple values are selected 如何删除存储在离子存储器中的单个键中的单个数据? - How to remove a single data which is store in a single key in ionic storage? 如何在本地存储中将多个 arrays 存储在单个 object 中并检索它? - How to store multiple arrays in single object in local storage and retrieve it? 如何将数据存储在本地存储中? - How to Store the Data in Local storage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM