- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 antd 模板进行表单设计。提交表单后,输入值没有被清除。
我试过this.props.form.resetFields()
它不工作得到以下错误
Unhandled Rejection (TypeError): Cannot read property 'resetFields' of undefined
codesandbox.io/s/funny-wright-oyd72?file=/src/App.js
import React, { Component, useState } from 'react';
import PropTypes from 'prop-types';
import {
Form,
Input, Layout,
Divider,
Tooltip,
Cascader,
Select,
Row,
Col,
Checkbox,
Button,
AutoComplete, InputNumber, DatePicker,
} from 'antd';
import axios from 'axios';
import { withRouter } from "react-router-dom";
import moment from 'moment';
import { QuestionCircleOutlined } from '@ant-design/icons';
import countries from './countrys.json'
import SweetAlert from 'sweetalert-react';
import 'sweetalert/dist/sweetalert.css';
const { TextArea } = Input;
const { Option } = Select;
const { Header, Content } = Layout;
const {form} = Form
class Registration extends Component {
constructor(props) {
super(props);
this.state = {
customerName: "",
username: "",
password: "",
address: "",
country: "",
state: "",
email: "",
contactNumber: null,
dob: "",
customerAge: null,
citizenStatus: "",
accountType: "",
branchName: "",
initDepositAmount: null,
initProofType: "",
initDocumentNo: "",
stateList: []
}
}
// handle change text
handleChangeText = (value, name) => {
this.setState({ [name]: value }, () => {
if (name == 'dob') {
this.handleChange_age(value)
}
})
}
handleChangeCountry = (countryName) => {
let countrList = countries.countries;
let countryObject = countrList.find(k => k.country == countryName);
this.setState({
...this.state,
stateList: countryObject.states,
country: countryName
})
}
//submit form
submitForm = () => {
const { stateList, ...withoutStateList } = this.state;
axios({
method: 'post',
url: 'http://localhost:3333/registration',
data: withoutStateList
}).then(response => {
this.setState({
show: true
});
// this.props.form.resetFields();
})
}
// Cancel form
navigateToLogin = () => {
this.props.history.push({ pathname: '/login' })
}
//Check age and Citizen status
handleChange_age = (dob) => {
let currentAge = Math.abs((moment().year()) - (moment(dob, "DD/MM/YYYY").year()));
let statusOfcitizen = null;
if (currentAge < 18) {
statusOfcitizen = "Minor";
} else if (currentAge > 18 && currentAge <= 60) {
statusOfcitizen = "Normal";
} else if (currentAge > 60) {
statusOfcitizen = "Senior";
}
this.setState({
...this.state,
customerAge: currentAge,
citizenStatus: statusOfcitizen
})
}
render() {
const formItemLayout = {
labelCol: {
xs: { span: 24 },
sm: { span: 9 },
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 6 },
},
};
const tailFormItemLayout = {
wrapperCol: {
xs: { span: 24, offset: 0, },
sm: { span: 21, offset: 0, },
},
};
function disabledDate(current) {
return current && current > moment().endOf('day');
}
return (
<div>
<Divider>New Registration</Divider>
<Form
{...formItemLayout}
name="register"
scrollToFirstError
onFinish={() => this.submitForm()}
ref={this.formRef}
>
<Form.Item
name="customerName"
label="Name"
rules={[
{
required: true,
message: 'Please input your name!',
whitespace: true
}, {
pattern: /^([a-z]+\s)*[a-z]+$/,
message: 'Please input alphabets only!',
}
]}
>
<Input onChange={e => this.handleChangeText(e.target.value, "customerName")} />
</Form.Item>
<Form.Item
name="username"
label="Username"
rules={[
{
required: true,
message: 'Please input your username!',
whitespace: true,
},
]}
>
<Input onChange={e => this.handleChangeText(e.target.value, "username")} />
</Form.Item>
<Form.Item
name="password"
label="Password"
rules={[
{
required: true,
message: 'Please input your password!',
},
]}
>
<Input.Password onChange={e => this.handleChangeText(e.target.value, "password")} />
</Form.Item>
<Form.Item
name="address"
label="Address"
rules={[
{
required: true,
message: 'Please input your Address!',
whitespace: true,
},
]}
>
<TextArea onChange={e => this.handleChangeText(e.target.value, "address")} />
</Form.Item>
<Form.Item
name="country"
label="Country"
rules={[
{
required: true,
message: 'Please input your Country!'
},
]}
>
<Select
showSearch
placeholder="Select a country"
optionFilterProp="children"
onChange={e => this.handleChangeCountry(e)}
filterOption={(input, option) =>
option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
}
>
{
countries.countries.map((cname, i) => {
return (
<Option value={cname.country} key={i}>{cname.country}</Option>
)
})
}
</Select>
</Form.Item>
<Form.Item
name="state"
label="State"
rules={[
{
required: true,
message: 'Please input your State!'
},
]}
>
<Select
showSearch
placeholder="Select a state"
optionFilterProp="children"
onChange={e => this.handleChangeText(e, "state")}
filterOption={(input, option) =>
option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
}
>
{
this.state.stateList.map((sname, i) => {
return (
<Option value={sname} key={i}>{sname}</Option>
)
})
}
</Select>
</Form.Item>
<Form.Item
name="email"
label="E-mail"
rules={[
{
type: 'email',
message: 'The input is not valid E-mail!',
},
{
required: true,
message: 'Please input your E-mail!',
},
]}
>
<Input onChange={e => this.handleChangeText(e.target.value, "email")} />
</Form.Item>
<Form.Item
name="contactNumber"
label="Contact Number"
// validateStatus={this.state.validateStatus}
// help={this.state.errorMsg}
rules={[
{
required: true,
message: 'Please input your contact number!',
type: 'number'
},
{
pattern: /^[2-9]{2}[0-9]{8}$/,
message: 'Please input valid contact number!',
}
]}
>
<InputNumber
min={0}
style={{ width: '100%' }}
onChange={e => this.handleChangeText(e, "contactNumber")}
/>
</Form.Item>
<Form.Item
name="dob"
label="Date Of Birth"
rules={[
{
required: true,
message: 'Please input your date of birth!'
},
]}
>
<DatePicker
format="DD/MM/YYYY"
disabledDate={disabledDate}
style={{ width: '100%' }}
onChange={e =>
this.handleChangeText(moment(e).format("DD/MM/YYYY"), "dob")
}
/>
</Form.Item>
<Form.Item
name="currentAge"
label="Your age is"
>
<Input value={this.state.customerAge} disabled />
<span></span>
</Form.Item>
<Form.Item
name="citizenStatus"
label="Citizen Status"
>
<Input value={this.state.citizenStatus} disabled />
<span></span>
</Form.Item>
<Form.Item
name="accountType"
label="Account Type"
rules={[
{
required: true,
message: 'Please input your account type!'
},
]}
>
<Select
showSearch
placeholder="Select a account type"
optionFilterProp="children"
filterOption={(input, option) =>
option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
}
onChange={e => this.handleChangeText(e, "accountType")}
>
<Option value="salary">Salary</Option>
<Option value="saving">Saving</Option>
</Select>
</Form.Item>
<Form.Item
name="branchName"
label="Branch Name"
rules={[
{
required: true,
message: 'Please input your branch name!',
whitespace: true,
},
]}
>
<Input onChange={e => this.handleChangeText(e.target.value, "branchName")} />
</Form.Item>
<Form.Item
name="initDepositAmount"
label="Initial Deposit Amount"
rules={[
{
required: true,
message: 'Please input your Initial Deposit Amount!'
},
]}
>
<InputNumber
min={1}
style={{ width: '100%' }}
onChange={e => this.handleChangeText(e, "initDepositAmount")}
/>
</Form.Item>
<Form.Item
name="initProofType"
label="Identiication Proof Type"
rules={[
{
required: true,
message: 'Please input your Identiication Proof Type!',
whitespace: true,
},
]}
>
<Input onChange={e => this.handleChangeText(e.target.value, "initProofType")} />
</Form.Item>
<Form.Item
name="initDocumentNo"
label="Identiication Document No"
rules={[
{
required: true,
message: 'Please input your Identiication Document No!',
whitespace: true,
},
]}
>
<Input onChange={e => this.handleChangeText(e.target.value, "initDocumentNo")} />
</Form.Item>
<Form.Item {...tailFormItemLayout}>
<Button type="primary" htmlType="submit">
Register
</Button>
<Button type="default" style={{ margin: '0 8px' }} onClick={this.navigateToLogin}>
Cancel
</Button>
</Form.Item>
</Form>
<SweetAlert
show={this.state.show}
title="Done"
text="Registered Successfully"
success
onConfirm={() => this.setState({ show: false })}
/>
</div>
);
}
}
Registration.propTypes = {
};
export default withRouter(Registration);
最佳答案
首先你不需要使用this.props
.当您像这样导入时。如果您仍然需要 Prop 形式,您需要Form.create
Ant 的api。 form
没有从 Form
导出api。更好的方法是你已经定义了 ref。通过以下方式访问表单值:
formRef = React.createRef();
<Form
{...formItemLayout}
name="register"
scrollToFirstError
onFinish={() => this.submitForm()}
ref={this.formRef}
>
//submit form
submitForm = () => {
console.log("Form submitted:", this.formRef, Form);
this.formRef.current.resetFields();
};
更新沙盒链接:
关于javascript - 在reactjs中提交后Antd清除表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63555646/
假设我有一个带有隐藏提交按钮的表单,我在其中输入值,然后我点击一个按钮,就会出现带有确认消息和确认按钮的对话框。当我单击“确认”按钮时,我还单击了表单中隐藏的提交按钮。这可能吗?我如何在 JQuery
我们正在学习 Git 并使用 GitHub 作为我们的托管站点。 我们都 fork upstream repo 并 PR 我们的提交到 upstream 以获取我们的更改。 我们正在努力学习如何压缩我
我只需要一些关于这段代码的帮助。 var prv3; var markIt3 = function(e) { if (prv3 === this && this.checked) { th
如果 1 个表单使用“GET”方法而另一个使用“POST”方法,我如何提交位于同一页面上的 2 个表单。每个表单都有相同的操作并转到相同的下一页。需要帮忙。感谢大家的帮助。 我怎样才能得到下面这两个使
您好,我的表单中有以下脚本 function pdf() { var frm = document.getElementById("form1"); frm.action = "http://www.
我有一个 iOS 胖静态库(iphoneos 和 iphonesimulator),如果我在应用程序提交期间使用它,它会因为二进制文件包含 iphonesimulator 代码而失败吗? 最佳答案 我
我似乎有一个卡住的 git repo。它卡在所有基本的添加、提交命令上,git push 返回所有内容为最新的。 从其他帖子我已经完成了 git gc 和 git fsck/ 我认为基本的调试步骤是
我正在尝试发送由 jquery 创建的表单。该表单附加到一个 div 中,下面的变量“data”是使用 php 创建的,我将只发布最重要的 js 代码。 我尝试了很多带有和不带有“on()”的操作,但
我面临一个简单的问题,但不知道如何解决。我正在使用 twitter bootstrap 的标签。选项卡有效,但每个选项卡中的表单不提交。表单在没有选项卡的情况下提交。 以下是我用于标签的链接
我的计算机上有 140 个 git 存储库,每周我可以处理其中 10-15 个。有没有办法知道是否忘记提交/推送我的一个项目? 这些存储库都位于同一位置:“C:/Projects”。 输出类似于 C:
我对 javascript 完全陌生,目前正在开发我的第一个函数。我有这 2 个文本输入区域,可以在其中输入他的姓名和级别。 Nom: Niveau (1 á 6): 提交后,
我安装了最新的 Docker CS,得到了 LAMP image来自 Docker 集线器。我正在尝试在其中创建一个数据库并使用保存在其中的数据库制作一个新图像。 启动容器:docker run --
我有这个 jQuery 简单代码: 由于某种原因,submit() 无法正常工作(我的表单在单击 old_thumb 按钮后未提交。有人可以帮助我吗? 这里是 html 的一部分(它很长
如何获得 input type="submit"onclick 事件来触发 commitfunds.valdiate?我不能使用类或 ID。它必须是一个 onclick 事件。 这是代码: row A
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
来自 this earlier thread我以为我知道可以使用 javascript submit() 命令通过 POST 方法发送表单数据。但我无法让它工作。这个演示在目的方面没有明显的意义,但请
在 mysql 重新启动时提交 XA 待处理事务时,出现以下错误。请帮助我解决这个错误。 mysql> XA RECOVER CONVERT XID; +----------+------------
我有一个带有 的表单. 如果启用了 Javascript,我将删除此 submit -输入字段$('#no-js-submit').remove();并添加“fire-ajax”按钮 $('Fire
我希望在页面加载后提交此表单,并且我使用了以下代码来完成此操作。问题是页面不断重新加载并停留在该循环中。 HTML Select Genre
我们有一个表单,其中有几个单独的提交按钮,它们执行不同的操作。问题是我有几个具有以下 HTML 的按钮: 现在您无法使用标准的 find_control 函数按值定位元素。所以我写了一个谓词函数来
我是一名优秀的程序员,十分优秀!