简体   繁体   English

如何在提交时获取多个文本字段的值?

[英]How do I get the values of multiple text fields on submit?

Background:背景:

I am trying to get the values that are currently in various text fields.我正在尝试获取当前在各种文本字段中的值。 I have lots of text fields on my page, and they are managed using a unified state/handler (please see code below).我的页面上有很多文本字段,它们使用统一的状态/处理程序进行管理(请参阅下面的代码)。

I have the submit button firing properly, but I don't know what to put in there in order to get the values from the text fields.我有正确触发提交按钮,但我不知道要放什么来从文本字段中获取值。

Problem:问题:

How do I properly set this up so that the submit function obtains the current values in each text field?如何正确设置,以便提交 function 获得每个文本字段中的当前值?

My Code:我的代码:

function handleSubmit() {
  // HOW DO I GET THE VALUES?
  console.log('CURRENT VALUE OF firstItem')
  console.log('CURRENT VALUE OF secondItem')
}

const [textFields, setTextFields] = useState({
  firstItem: "",
  secondItem: ""
});

const handleChangeTextField = (type) => (value) => {
  setTextFields((prevState) => ({
    ...prevState,
    [type]: value
  }));
};

<TextField
  id="firstItem"
  variant="filled"
  value={textFields.firstItem}
  onChange={handleChangeTextField("firstItem")}
/>
<TextField
  id="secondItem"
  variant="filled"
  value={textFields.secondItem}
  onChange={handleChangeTextField("secondItem")}
/>

Thanks!谢谢!

The textfield values are stored in state, so you would access them via the textFields state variable.文本字段值存储在 state 中,因此您可以通过textFields state 变量访问它们。

function handleSubmit() {
  console.log('CURRENT VALUE OF firstItem', textFields.firstItem);
  console.log('CURRENT VALUE OF secondItem', textFields.secondItem);
}

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

相关问题 如何使用跨度中的值填充隐藏文本字段以访问这些值? - How do I get populate hidden text fields with values from spans in order to access the values? 我怎样才能从多个表单中获取值并提交它们? - how can i get values from multiple forms and submit them? Javascript获取多个文本字段的值 - Javascript get values of multiple text fields 如何清除文本输入字段中的值? - how do I clear the values in my text input fields? 如何使用统一的状态/处理程序编辑多个文本字段? - How do I edit multiple text fields with a unified state/handler? 单击提交后,如何提醒用户在HTML表单中多个字段留空? - How do I alert the user that multiple fields are left blank in an HTML form after clicking submit? 一旦不再关注任何字段,如何提交具有多个字段的表单? - How do I submit a form with multiple fields as soon as no field is focused anymore? 如何从单个表单元素中的多个字段集中获取输入字段(包括选择,文本区域)的所有值? - How do I get all the value of Input fields (including select, text-area) from multiple fieldsets inside a single form element? 如何使用所有文本字段的值获取警报? - How to get an alert with the values of all text fields? 如何在一个文本字段中输入的文本实时复制到多个文本字段? - How can I get text entered in one text field copied in realtime to multiple text fields?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM