单击表单按钮时,Ant Design 表单元素未提交
Ant Design form element is not submitting when the form button is clicked
我正在构建一个 Web 应用程序,在后端使用 Django Rest 框架,在前端使用 React。我还使用 Ant Design 来帮助设计样式。我一直在关注 Youtube 上的教程,但我目前 运行 在尝试提交表单以创建新文章时遇到一些问题。我已经解决了一些问题,我相信问题出在表单的 onSubmit 函数上。我尝试向按钮添加一个 onClick 以确保点击被识别并且按预期工作,这就是为什么我认为问题出在 onSubmit 上。现在,我要做的就是将表单元素打印到控制台。我是开发 Web 应用程序的超级新手,因此非常感谢任何帮助。
import React from 'react';
import { Form, Input, Button } from 'antd';
class CustomForm extends React.Component {
handleFormSubmit = (event, requestType, articleID) => {
//event.preventDefault();
const title = event.target.elements.title.value;
const content = event.target.elements.content.value;
console.log(title, content);
}
render() {
return (
<div>
<Form
onSubmit={event =>
this.handleFormSubmit(
event,
this.props.requestType,
this.props.articleID
)
}
>
<Form.Item label="Title">
<Input name="title" placeholder="Enter a Title" />
</Form.Item>
<Form.Item label="Content">
<Input name="content" placeholder="Enter the content of the announcement here" />
</Form.Item>
<Form.Item>
<Button type="primary" htmlType="submit">{this.props.btnText}</Button>
</Form.Item>
</Form>
</div>
);
}
};
export default CustomForm;
根据 antd
文档,问题是 onSubmit
,这里应该是 onFinish
,您还需要在 [中传递 name
和 label
=16=]:
代码如下:
const Demo = () => {
const onFinish = (values) => {
console.log("Success:", values);
//Can directly call props here
};
const onFinishFailed = (errorInfo) => {
console.log("Failed:", errorInfo);
};
return (
<Form
{...layout}
name="basic"
initialValues={{
remember: true
}}
onFinish={onFinish}
onFinishFailed={onFinishFailed}
>
<Form.Item label="Title" name="title">
<Input name="title" placeholder="Enter a Title" />
</Form.Item>
<Form.Item label="Content" name="content">
<Input
name="content"
placeholder="Enter the content of the announcement here"
/>
</Form.Item>
<Form.Item>
<Button type="primary" htmlType="submit">
Submit
</Button>
</Form.Item>
</Form>
);
};
这是演示:https://codesandbox.io/s/competent-violet-opwlh?file=/index.js
在我的例子中,我使用了普通的 <form>
和 Ants 控制的 <Input>
元素,因为没有提交表单,也没有控制输入。
/**
* Component's state
*/
interface LoginSignUpState {
username: string,
password: string
}
export class LoginSignUpForm extends React.Component<{}, LoginSignUpState> {
constructor (props) {
super(props)
this.state = {
username: '',
password: ''
}
}
/**
* Updates the component state from the user input
* @param e Change event
*/
handleInputChange = (e) => {
this.setState<never>({ [e.target.name]: e.target.value })
}
/**
* Checks that the login form is valid!
* @returns True if the login form is valid, false otherwise
*/
loginFormIsValid (): boolean {
return this.state.username.trim().length > 0 &&
this.state.password.trim().length > 0
}
handleSubmit = (e) => {
if (!this.loginFormIsValid()) {
e.preventDefault()
}
}
render () {
return (
<form
method='POST'
onSubmit={this.handleSubmit}
action={urlAuthenticate}
>
<Input
prefix={<UserOutlined className="site-form-item-icon" />}
placeholder="Username"
className='margin-bottom-2'
name='username'
value={this.state.username}
onChange={this.handleInputChange}
/>
<Input
prefix={<LockOutlined className="site-form-item-icon" />}
value={this.state.password}
type="password"
className='margin-bottom-2'
name='password'
placeholder="Password"
onChange={this.handleInputChange}
/>
<Button type="primary" htmlType="submit" disabled={!this.loginFormIsValid()}>
Log in
</Button>
</form>
)
}
}
我认为 Ants 的例子是
我正在构建一个 Web 应用程序,在后端使用 Django Rest 框架,在前端使用 React。我还使用 Ant Design 来帮助设计样式。我一直在关注 Youtube 上的教程,但我目前 运行 在尝试提交表单以创建新文章时遇到一些问题。我已经解决了一些问题,我相信问题出在表单的 onSubmit 函数上。我尝试向按钮添加一个 onClick 以确保点击被识别并且按预期工作,这就是为什么我认为问题出在 onSubmit 上。现在,我要做的就是将表单元素打印到控制台。我是开发 Web 应用程序的超级新手,因此非常感谢任何帮助。
import React from 'react';
import { Form, Input, Button } from 'antd';
class CustomForm extends React.Component {
handleFormSubmit = (event, requestType, articleID) => {
//event.preventDefault();
const title = event.target.elements.title.value;
const content = event.target.elements.content.value;
console.log(title, content);
}
render() {
return (
<div>
<Form
onSubmit={event =>
this.handleFormSubmit(
event,
this.props.requestType,
this.props.articleID
)
}
>
<Form.Item label="Title">
<Input name="title" placeholder="Enter a Title" />
</Form.Item>
<Form.Item label="Content">
<Input name="content" placeholder="Enter the content of the announcement here" />
</Form.Item>
<Form.Item>
<Button type="primary" htmlType="submit">{this.props.btnText}</Button>
</Form.Item>
</Form>
</div>
);
}
};
export default CustomForm;
根据 antd
文档,问题是 onSubmit
,这里应该是 onFinish
,您还需要在 [中传递 name
和 label
=16=]:
代码如下:
const Demo = () => {
const onFinish = (values) => {
console.log("Success:", values);
//Can directly call props here
};
const onFinishFailed = (errorInfo) => {
console.log("Failed:", errorInfo);
};
return (
<Form
{...layout}
name="basic"
initialValues={{
remember: true
}}
onFinish={onFinish}
onFinishFailed={onFinishFailed}
>
<Form.Item label="Title" name="title">
<Input name="title" placeholder="Enter a Title" />
</Form.Item>
<Form.Item label="Content" name="content">
<Input
name="content"
placeholder="Enter the content of the announcement here"
/>
</Form.Item>
<Form.Item>
<Button type="primary" htmlType="submit">
Submit
</Button>
</Form.Item>
</Form>
);
};
这是演示:https://codesandbox.io/s/competent-violet-opwlh?file=/index.js
在我的例子中,我使用了普通的 <form>
和 Ants 控制的 <Input>
元素,因为没有提交表单,也没有控制输入。
/**
* Component's state
*/
interface LoginSignUpState {
username: string,
password: string
}
export class LoginSignUpForm extends React.Component<{}, LoginSignUpState> {
constructor (props) {
super(props)
this.state = {
username: '',
password: ''
}
}
/**
* Updates the component state from the user input
* @param e Change event
*/
handleInputChange = (e) => {
this.setState<never>({ [e.target.name]: e.target.value })
}
/**
* Checks that the login form is valid!
* @returns True if the login form is valid, false otherwise
*/
loginFormIsValid (): boolean {
return this.state.username.trim().length > 0 &&
this.state.password.trim().length > 0
}
handleSubmit = (e) => {
if (!this.loginFormIsValid()) {
e.preventDefault()
}
}
render () {
return (
<form
method='POST'
onSubmit={this.handleSubmit}
action={urlAuthenticate}
>
<Input
prefix={<UserOutlined className="site-form-item-icon" />}
placeholder="Username"
className='margin-bottom-2'
name='username'
value={this.state.username}
onChange={this.handleInputChange}
/>
<Input
prefix={<LockOutlined className="site-form-item-icon" />}
value={this.state.password}
type="password"
className='margin-bottom-2'
name='password'
placeholder="Password"
onChange={this.handleInputChange}
/>
<Button type="primary" htmlType="submit" disabled={!this.loginFormIsValid()}>
Log in
</Button>
</form>
)
}
}
我认为 Ants 的例子是