【发布时间】:2019-11-13 00:42:22
【问题描述】:
我无法为表单中的输入字段设置值。下面是我的代码。甚至我也试图在输入元素中的value='ABC' 中给出直接值。但没有运气。当我尝试在表单标签之外显示值时,例如
<h1>{this.state.company.companyCode}</h1>
这显示了价值。但不在 Form 内。
class UpdateCompany extends Component {
constructor(props) {
super(props);
this.state = {
companycode: { value: ''},
company: ''
};
this.loadCompanydetail = this.loadCompanydetail.bind(this);
}
loadCompanydetail(companyid) {
GetCompanyById(companyid)
.then(response => {
this.setState({
company: response
});
}).catch(error => {
if(error.status === 404) {
this.setState({
notFound: true,
isLoading: false
});
} else {
this.setState({
serverError: true,
isLoading: false
});
}
});
}
componentDidMount(){
const companyid =this.props.match.params.companyId;
this.loadCompanydetail(companyid);
}
render() {
const { getFieldDecorator } = this.props.form
return (
<h1 className="page-title">Edit Company - {this.state.company.companyCode}</h1>
<Form>
<Form.Item label="Company Code">
{getFieldDecorator('companycode', {
rules: [
{
required: true,
message: 'Please enter Code',
max: 3,
},
],
})(<Input name="companycode" value={this.state.company.companyCode} />)}
</Form.Item>
<Form.Item wrapperCol={{ span: 12, offset: 6 }}>
<Button type="primary"
htmlType="submit"
size="large">Submit</Button>
</Form.Item>
</Form>
);
}
}
【问题讨论】:
-
您似乎在使用 Ant-Design。您能否包括您的导入、标签问题和调整标题?
标签: javascript reactjs forms input