reactjs - 用ant-design在react js中提交后清除表单输入字段值
问题描述
我使用 react 创建了一个注册页面。在那里,我使用了以下注册表单。https://ant.design/components/form。所有验证均已正确处理,成功尝试后用户可以注册到系统。我遇到的唯一问题是,提交后无法清除表单输入字段值。
我已经实现了一种将表单字段值清除为空的方法。但它不起作用。而且我在stackoverflow中尝试了以前的类似问题,但仍然无法为我找到一个可行的问题。原因可能是因为我使用的是 ant 设计模板。
this.setState({
confirmDirty: false,
autoCompleteResult: [],
userName: '',
email: '',
experience: [],
password: ''
})
上面的代码是清除输入值。但它不起作用,所有表单输入字段值仍然保存。下面是注册表单代码的一部分。
class RegistrationForm extends React.Component {
state = {
confirmDirty: false,
autoCompleteResult: [],
userName: '',
email: '',
experience: [],
password: ''
};
//below form is inside the render method and return
<Form onSubmit={this.handleSubmit}>
<FormItem
{...formItemLayout}
label="E-mail"
>
{getFieldDecorator('email', {
rules: [{
type: 'email', message: 'The input is not valid E-mail!',
}, {
required: true, message: 'Please input your E-mail!',
}],
})(
<Input />
)}
</FormItem>
</Form>
handleSubmit = (e) => {
e.preventDefault();
this.props.form.validateFieldsAndScroll((err, values) => {
if (!err) {
//submission done
//then execute the above code which I mentioned previously in the question, to reset the input fields value
}
});
}
}
我可能在哪里出错,我该如何解决?
解决方案
我们可以使用 ant design libraryresetFields
提供的 form props 中的函数来清除表单数据。
表单提交成功后,使用this.props.form.resetFields()
清除表单中的数据。
代码:
const { Form, Input, Tooltip, Icon, Cascader, Select, Row, Col, Checkbox, Button, AutoComplete, } = antd;
const { Option } = Select;
const AutoCompleteOption = AutoComplete.Option;
class RegistrationForm extends React.Component {
state = {
confirmDirty: false,
autoCompleteResult: [],
};
handleSubmit = (e) => {
e.preventDefault();
this.props.form.validateFieldsAndScroll((err, values) => {
if (!err) {
console.log('Received values of form: ', values);
}
});
}
handleConfirmBlur = (e) => {
const value = e.target.value;
this.setState({ confirmDirty: this.state.confirmDirty || !!value });
}
render() {
const { getFieldDecorator } = this.props.form;
const { autoCompleteResult } = this.state;
const formItemLayout = {
labelCol: {
xs: { span: 24 },
sm: { span: 8 },
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 16 },
},
};
const tailFormItemLayout = {
wrapperCol: {
xs: {
span: 24,
offset: 0,
},
sm: {
span: 16,
offset: 8,
},
},
};
return (
<Form onSubmit={this.handleSubmit}>
<Form.Item
{...formItemLayout}
label="E-mail"
>
{getFieldDecorator('email', {
rules: [{
type: 'email', message: 'The input is not valid E-mail!',
}, {
required: true, message: 'Please input your E-mail!',
}],
})(
<Input />
)}
</Form.Item>
<Form.Item
{...formItemLayout}
label="Password"
>
{getFieldDecorator('password', {
rules: [{
required: true, message: 'Please input your password!',
}, {
validator: this.validateToNextPassword,
}],
})(
<Input type="password" />
)}
</Form.Item>
<Form.Item {...tailFormItemLayout}>
{getFieldDecorator('agreement', {
valuePropName: 'checked',
})(
<Checkbox>I have read the <a href="">agreement</a></Checkbox>
)}
</Form.Item>
<Form.Item {...tailFormItemLayout}>
<Button type="primary" htmlType="submit">Register</Button>
</Form.Item>
<Form.Item {...tailFormItemLayout}>
<Button onClick={e => {
this.props.form.resetFields()
}} >Clear</Button>
</Form.Item>
</Form>
);
}
}
const WrappedRegistrationForm = Form.create()(RegistrationForm);
ReactDOM.render(<WrappedRegistrationForm />, mountNode);
希望能帮助到你 :)
推荐阅读
- python - Python 2d 列表到字典列表
- parallel-processing - WebGL - 从纹理读取后有缺失值
- excel - Excel中的选择性下拉人口
- wordpress - 删除插件类中定义的操作
- python - docker-py 中 docker port CONTAINER 的等效命令是什么
- python - Webscraper 没有得到文本
- linux-kernel - PCIe 内核驱动程序 DMA 写入不正确的地址
- python - Python将标签输入笔记本部分
- angular - 架构验证失败并出现以下错误:数据路径“”应该具有必需的属性“tsConfig”
- python - 谁能解释一下这两个代码之间的区别