简体   繁体   English

将多个表单输入值以数组的形式存储在 JavaScript 中的本地存储中

[英]Store multiple form input values in local storage in JavaScript in the form of an array

I am trying to save input values submitted through form into local storage in the form of the array, but once the form is submitted again, the saved value in local storage gets changed with the new submitted value as the我正在尝试将通过表单提交的输入值以数组的形式保存到本地存储中,但是一旦再次提交表单,本地存储中保存的值就会更改为新提交的值作为

function bookDetails(bookName,bookAuthor,bookType) {
    this.name = bookName;
    this.author = bookAuthor;
    this.type = bookType;
}

let libraryForm = document.getElementById("bookForm");
libraryForm.addEventListener("submit", libraryBooksDetails);

function libraryBooksDetails(e,index) {
    
    e.preventDefault();
    console.log("The book details have been submitted");
    let bookName = document.getElementById("bookName").value;
    let bookAuthor = document.getElementById("author").value;
    let bookType;
    let fiction = document.getElementById("Fiction"); 
    let programming = document.getElementById("ComputerProgramming");
    let personal = document.getElementById("PersonalDevelopement");
    
    if(fiction.checked){
        bookType = fiction.value;
    }
    else if(programming.checked){
        bookType = programming.value;
    }
    else if(personal.checked){
        bookType = personal.value;
    }
// BookDetails Object
    let book = new bookDetails(bookName,bookAuthor,bookType);
    
    let bookData = ""
    bookData = bookData || [];
    let nameOfBooks = bookData.concat(bookName)
    // let nameOfBooks = bookData
    // nameOfBooks.push(book.bookName);
    localStore.setItem("books",JSON.stringify(nameOfBooks))
    console.log(book);

    let display = new Display()
    display.add(book);
    display.clear();
}

Thanks谢谢

I'm not sure I understand the question completely because you are setting the value let book = new bookDetails but that is not what you are storing in local storage.我不确定我是否完全理解这个问题,因为您正在设置值let book = new bookDetails但这不是您在本地存储中存储的内容。

 // First time bookData value is set, this is always an empty string let bookData = ""; // Since empty string is a falsey value, bookData value is re-set to an empty array bookData = bookData || []; // nameOfBooks is initialised as an array with one value of bookName let nameOfBooks = bookData.concat(bookName); // books key in local storage is a stringified array with one item localStore.setItem("books", JSON.stringify(nameOfBooks));

localStorage.setItem(key, value) will always replace the existing value at that key. localStorage.setItem(key, value)将始终替换该键的现有值。 If you want to append to the stored value, first get the value and parse it before appending the new value.如果要将 append 到存储的值,请先获取该值并对其进行解析,然后再附加新值。

 const oldValue = localStorage.getItem('books'); const parsed = JSON.parse(oldValue); const newValue = parsed.concat(nameOfBooks); localStorage.setItem('books', JSON.stringify(newValue));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM