Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
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.

about 4 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); });

about 4 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 Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!