• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

158
Views
¿Cómo puedo mantener los datos de un formulario en mi javascript si el botón de enviar vuelve a cargar la página?

Tengo un formulario simple con 2 entradas de texto, 1 número y 1 radio, y estoy tratando de tomar esos datos en mi javascript para crear una matriz de objetos creados con un constructor, pero en el momento presiono el botón de enviar y consola log the array aparece vacío, supongo que por la recarga de la página, pero no estoy seguro.

Aquí está mi código javascript:

 let myLibrary = []; //the array I wantd to fill with the objects const form = document.querySelector('#form'); //the form itself // the constructor function Book (title, author, pages, read) { this.title = title; this.author = author; this.pages = pages; this.read = read; } // the submit listener, which takes the data of the form and sends it as argument of the function that pushes the new object to the array form.addEventListener('submit', (e) => { const data = Object.fromEntries(new FormData(e.target).entries()); addBookToLibrary(data); }); function addBookToLibrary (data) { myLibrary.push(new Book(data.title, data.author, data.pages, data.read)); }

He intentado aplicar el método preventDefault() al botón de envío, pero eso detiene el envío del formulario. También intenté hacerlo con FormData() pero tuve el mismo problema.

almost 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Lo que hice:

  1. Cambió el botón de enviar y agregó un botón de tipo botón normal para evitar que la página se vuelva a cargar.
  2. Tome los datos del formulario con la propiedad "elementos" del formulario HTML, que devuelve una lista de todos los controles del formulario y se puede acceder con un índice -> https://developer.mozilla.org/en-US/docs /Web/API/HTMLFormElement/elementos
  3. Cree los objetos pasando el atributo "valor" de cada elemento como argumentos del constructor y empújelo a la matriz
  4. Cree un detector de clics para el botón y llame a la función que crea los objetos.

 let myLibrary = []; const data = document.querySelector('#form').elements; // HTMLFormElement.elements const btnSubmit = document.querySelector('#btnSubmit'); // Button type button function Book (title, author, pages, read) { this.title = title; this.author = author; this.pages = pages; this.read = read; } // Take the "value" of each element of the form and create a new Book object function addBookToLibrary (data) { myLibrary.push(new Book(data[0].value, data[1].value, data[2].value, data[3].value)); console.log(myLibrary); } // The listener that calls the function btnSubmit.addEventListener('click', () => { addBookToLibrary(data); });

almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error