【发布时间】:2020-05-10 20:51:35
【问题描述】:
我有一个玩家表单,它接受多个玩家输入,并且在提交时,所有玩家都被添加到一个 mongodb 集合中。我不确定处理多个输入的最佳方法是什么。到目前为止,下面的代码只有一个玩家的输入。如果我想添加多个输入以添加最多 10 个玩家,每个输入字段是否需要具有唯一的名称?如果是这样,它将如何适合我的 addPlayerToTeam 函数?提前致谢!
export default class AddPlayersForm extends Component {
constructor(props) {
super(props);
this.submitForm = this.submitForm.bind(this);
}
submitForm(event) {
event.preventDefault();
this.props.addPlayerToTeam({
firstName: event.target.firstName.value,
lastName: event.target.lastName.value,
email: event.target.email.value,
height: event.target.height.value,
})
}
render() {
return (
<div>
<h2>Add players</h2>
<h4>Must have a minimum of 5 players to complete registration</h4>
<Table>
<Form onSubmit={(event) => this.submitForm(event)}>
<FormGroup>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Height</th>
</tr>
<InputGroup>
<tr>
<td><Input type="text" name="firstName" id="firstName" /></td>
<td><Input type="text" name="lastName" id="lastName" /></td>
<td><Input type="text" name="email" id="email" /></td>
<td><Input type="text" name="height" id="height" /></td>
</tr>
</InputGroup>
</thead>
</FormGroup>
</Form>
</Table>
</div>
)
}
}
【问题讨论】:
标签: reactjs forms input onsubmit