简体   繁体   English

如果提交按钮重新加载页面,我如何将表单的数据保留在我的 javascript 中

[英]How can I keep the data of a form in my javascript if the submit button reloads the page

I have a simple form with 2 text, 1 number and 1 radio inputs, and I'm trying to take that data into my javascript to create an array of objects created with a constructor, but in the moment I hit the submit button and console log the array it appears empty, I suppose because of the reloading of the page, but I'm not sure.我有一个带有 2 个文本、1 个数字和 1 个单选输入的简单表单,我正在尝试将这些数据带入我的 javascript 以创建一个使用构造函数创建的对象数组,但在我点击提交按钮和控制台的那一刻记录它显示为空的数组,我想是因为重新加载了页面,但我不确定。

Here is my javascript code:这是我的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)); }

I've tried applying the preventDefault() method to the submit button but that stop the submitting of the form.我尝试将 preventDefault() 方法应用于提交按钮,但这会停止提交表单。 I also tried doing it with FormData() but I had the same issue.我也尝试过使用 FormData() 但我遇到了同样的问题。

What I did:我做了什么:

  1. Changed the submit button and add a regular button type button to prevent the page to reload.更改了提交按钮并添加了常规按钮类型按钮以防止页面重新加载。
  2. Take the data of the form with the "elements" property of the HTML form, which returns a list of all the form controls and can be accessed with an index -> https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/elements使用 HTML 表单的“元素”属性获取表单的数据,该属性返回所有表单控件的列表,可以通过索引访问 -> https://developer.mozilla.org/en-US/docs /Web/API/HTMLFormElement/元素
  3. Create the objects passing the "value" attribute of each element as arguments of the constructor, and push it into the array创建将每个元素的“value”属性作为构造函数的参数传递的对象,并将其推送到数组中
  4. Create a click listener for the button, and call the function that creates the objects.为按钮创建一个点击监听器,并调用创建对象的函数。

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

暂无
暂无

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

相关问题 在页面上重新加载表单后,如何编写JavaScript cookie以保持数据持久性? - How can I write JavaScript cookies to keep the data persistent after page reloads on my form? HTML表单提交按钮不起作用,仅重新加载页面 - HTML Form Submit button doesn't work, only reloads page 在Wordpress中使用javascript提交表单后,页面会立即重新加载 - Page reloads immediately after form submit using javascript in wordpress 单击表单的提交按钮时,如何刷新页面? - How can I refresh the page when the submit button of a form is clicked? 当您有一个重新加载页面的按钮时,如何使 javascript 保留 URL 中的参数 - How do you make javascript keep the parameters in the URL when you have a button that reloads the page 如何使用 JavaScript 函数根据单选按钮选择让我的表单提交按钮打开不同的 URL? - How can I make my form submit button open a different URL based on radio button selection using JavaScript function? 当我按下提交按钮时,我的页面会保持刷新 - my page keep refreshing when i press the submit button JavaScript:提交表单后,如何保持单选按钮处于选中状态? - JavaScript: How do I keep a radio button checked after form submit? 如何从另一个页面提交包含现有数据的表单? - How can I submit a form from another page with existing data? 如何获取电子邮件页面并向其提交表单数据? - How can I fetch an email page and submit form data to it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM