简体   繁体   English

如何使用 javascript 从表单中收集数据并将其存储在变量中?

[英]How can i collect data from form with javascript and store it in variables?

Hey lets say i have a simple form like this:嘿,可以说我有一个像这样的简单表格:

<form>
<label for="name">Enter name:</label>
<input type="text" id="name">
<label for="surname">Enter surname:</label>
<input type="text" id="surname">
<label for="number">Phone number:</label>
<input type="text" id="number">
</form>

and in javascript, instead of getting values one by one and storing them with queryselectors, is there a way i can select the whole form and get its input values?在javascript中,不是一个一个地获取值并将它们存储在查询选择器中,有没有一种方法可以选择整个表单并获取其输入值? also after that how can i store them in variables so i can use them later?在那之后我如何将它们存储在变量中以便以后可以使用它们? for example, print them one by one.例如,一张一张打印出来。

You can use FormData :您可以使用FormData

 let myForm = document.getElementById('myForm'); let formData = new FormData(myForm); console.log(formData.entries());
 <form id="myForm"> <label for="name">Enter name:</label> <input type="text" id="name"> <label for="surname">Enter surname:</label> <input type="text" id="surname"> <label for="number">Phone number:</label> <input type="text" id="number"> </form>

Bobi.博比。 You may use javascript FormData() method.您可以使用 javascript FormData() 方法。

var formData = new FormData(document.querySelector('form'))

Please check this document.请检查此文件。 https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData

Hope this helps.希望这可以帮助。 Happy coding~ :)编码愉快~ :)

暂无
暂无

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

相关问题 如何使用这个 React 组件来收集表单数据? - How can I use this React component to collect form data? 如何从表单容器中的文本字段获取和存储数据? - How can I get and store Data from Textfield in Form Container? 我有一个表单,用于收集用户需要使用JavaScript停止的数据 - I have a form that collect data from a user that I need to stop with JavaScript 如何从 HTML 表格中收集数据 - How to collect data from HTML form 我可以使用JavaScript fetch将表单数据存储在内存中吗? - Can I use the JavaScript fetch to store form data in-memory? 如何在本地存储中以及从本地存储中存储和检索许多JavaScript变量 - How can I store and retrieve many javascript variables in and from local storage 如何使用 localStorage 在 javascript 和 html 的表单中存储值/数据 - How can I use localStorage to store the value/data IN a form in javascript and html 如何从HTML表单获取用户输入并将其存储在现有的JavaScript数组中 - How can I get the user input from a HTML form and store it in an existing JavaScript array 如何使此“配置文件”表单本地存储数据? Javascript - How do I make this 'profile' form localy store the data? Javascript 您如何使用表单从本地 HTML 文件中收集数据并将其存储在本地(这样就无需担心服务器)? - How do you collect data from a local HTML file with a form and store it locally(so that there is no need to worry about servers)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM