【发布时间】:2020-11-23 15:32:38
【问题描述】:
我无法获得 React Ant Form Select 值,因为它未定义。我试过更新状态和任何东西,但没有解决这个问题。错误位于 handleChange 方法下。也许我正在处理错误的 Ant 设计形式。 这里可能有什么问题?
我的表单代码:
import React from "react";
import { Form, Input, Button, Select } from "antd";
import axios from "axios";
const FormItem = Form.Item;
const { Option } = Select;
class CustomForm extends React.Component {
constructor(props) {
super(props)
this.state = {sport: ''};
this.handleChange = this.handleChange.bind(this);
this.handleFormSubmit = this.handleFormSubmit.bind(this);
}
handleChange(event) {
const target = event.target;
const value = target.value;
const name = target.name;
this.setState({
[name]: value
});
}
handleFormSubmit = (event, requestType, trainingID) => {
const sport = event.targets.elements.sport.value;
...
}
render() {
return (
<div>
<Select name="sport" value={this.state.sport} style={{ width: 120 }} onChange={this.handleChange}>
<Option value="jooks" >Jooks</Option>
<Option value="jõud" >Jõud</Option>
<Option value="crossfit" >Crossfit</Option>
<Option value="kardio" >Kardio</Option>
</Select>
</FormItem>
<FormItem>
<Button type="primary" htmlType="submit" shape="round" >
{this.props.btnText}
</Button>
</FormItem>
</Form>
</div>
);
}
}
【问题讨论】:
标签: reactjs forms state antd react-props