表单提交

import React from "react";
import { Input, Form, Button } from "antd";

class FormSubmit extends React.PureComponent {
  static defaultProps = {
    onSubmit() {}
  };

  handleSubmit = e => {
    e.preventDefault();

    const { form, onSubmit } = this.props;
    const { validateFields, validateFieldsAndScroll } = form;

    validateFields((error, values) => {
      if (!error) {
        onSubmit(values);
      }
    });

    validateFieldsAndScroll((error, values) => {
      if (!error) {
        onSubmit(values);
      }
    });
  };

  render() {
    const { form } = this.props;
    const { getFieldDecorator } = form;
    return (
      <Form onSubmit={this.handleSubmit}>
        <Form.Item label="用户名">
          {getFieldDecorator("username"})(<Input />)}
        </Form.Item>
        <Form.Item label="密码">
          {getFieldDecorator("password")(<Input type="password" />)}
        </Form.Item>
        <Form.Item>
          <Button type="primary" htmlType="submit">
            Submit
          </Button>
        </Form.Item>
      </Form>
    );
  }
}

export default Form.create()(FormSubmit);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

使用onSubmit可以在填写完表单后可以直接按回车键提交表单,这在用于查询条件填写时非常好用,但需要阻止表单的默认方法,不然会刷新浏览器

上次更新: 11/5/2019, 6:30:17 AM