简体   繁体   English

从映射的 forms 通过 reac 中的 e.target 获取值

[英]get value through e.target in reac for from mapped forms

I am displaying multiple forms by mapping in react, and submit button is in parent component, how to getvalues of all the forms which are mapped on form submit?我通过在反应中映射显示多个 forms,并且提交按钮在父组件中,如何获取映射到表单提交的所有 forms 的值?

below is my code for parent component:以下是我的父组件代码:

<form>
     {rowdata.map((item, index) => <ChildForm key={index} {...this.state} />)}
     <Button type="submit" className="primarybtn btnsmall"/>
         </form>    

Put the entire form inside a the component and simply map the component with the form and button inside of it.将整个表单放入组件中,然后简单地 map 组件,其中包含表单和按钮。

Just pass props down.只是传递道具。

Parent Component.父组件。

return (
{someArray.map(form => (
     <SomeForm onSubmit={this.formSubmitHandler} key={form.key} />

)}

Child Component.子组件。

import React from 'react;

function SomeForm(props) {
return (
<div>
 <form onSubmit={props.onSubmitHandler}>
   Your Form Here
   <Button type="submit" className="primarybtn btnsmall"/>
</form>
 )}

Why are you using forms inside of forms.为什么在 forms 内部使用 forms。 Maybe you should use different input modes and a single button to submit the input values.也许您应该使用不同的输入模式和一个按钮来提交输入值。 For getting the values you can construct a array of objects( one object for one Iteration the map function)为了获取值,您可以构造一个对象数组(一个 object 用于一次迭代 map 函数)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM