如何从Ant设计中的“选择/选择”组件中获取价值

时间:2019-04-22 02:18:56

标签: javascript html reactjs antd

我想从我的选择中检索值,这样我可以发出后期请求。从文本输入中获取内容没有问题,但是由于某种原因,我无法从下拉菜单中选择它。我最终得到一个 "TypeError: Cannot read property 'value' of undefined"

这是我正在使用的代码。

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

const { Option } = Select;

class ItemForm extends React.Component {

  handleFormSubmit = event => {
    event.preventDefault();
    const name = event.target.elements.name.value;
    const description = event.target.elements.description.value;
    const category = event.target.elements.category.value;
    console.log(name, description, this.refs.category.value);
  };

  render() {
    return (
      <div>
        <Form onSubmit={this.handleFormSubmit}>
          <Form.Item label="Form Layout" />
          <Form.Item label="Product Name">
            <Input name="name" placeholder="Ex: Organic Apple..." />
          </Form.Item>
          <Form.Item label="Description">
            <Input name="description" placeholder="Ex: Juicy organic apples!" />
          </Form.Item>
          <Form.Item label="Category">
            <Select name="category" placeholder="Please select a category">
              <Option value="Fruit">Fruit</Option>
              <Option value="Vegetable">Vegetable</Option>
              <Option value="Poultry">Poultry</Option>
            </Select>
          </Form.Item>
          <Form.Item>
            <Button type="primary" htmlType="submit">
              Submit
            </Button>
          </Form.Item>
        </Form>
      </div>
    );
  }
}
export default ItemForm;

5 个答案:

答案 0 :(得分:1)

使用onChange,当select的值更改时将触发它。 antd select documentation

<Form.Item label="Category">
  <Select 
    onChange={(value) => {
      alert(value)
    }} 
    name="category" 
    placeholder="Please select a category">
      <Option value="Fruit">Fruit</Option>
      <Option value="Vegetable">Vegetable</Option>
      <Option value="Poultry">Poultry</Option>
  </Select>
</Form.Item>

working example

答案 1 :(得分:1)

您打算使用与经典javascript方法类似的方法,可以使用getFieldValue
但是如下所述耦合到相干的createRefFormForm.Item

获取值时,请记住引用Form.Item的名称,而不要引用Input的名称;-)

我创建了一个sandbox demo,希望其他人会喜欢或贡献。

import React from "react";
import { Form, Input, Button, Select } from "antd";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css"; //export default ItemForm;

const { Option } = Select;

class ItemForm extends React.Component {
  formRef = React.createRef();

  handleFormSubmit = event => {
    event.preventDefault();
    console.log("All field values", this.formRef.current.getFieldsValue());
    const name = this.formRef.current.getFieldValue("productName"); //OLD event.target.elements.name.value;
    const description = this.formRef.current.getFieldValue("description"); //OLD event.target.elements.description.value;
    const category = this.formRef.current.getFieldValue("category"); //OLD event.target.elements.category.value;
    console.log(name, description, category);
    alert(`${name}, ${description}, ${category}`);
  };

  render() {
    return (
      <div>
        <Form ref={this.formRef} onSubmit={this.handleFormSubmit}>
          <Form.Item label="Form Layout (Form.Item-createRef-getFieldValue Example)" />
          <Form.Item label="Product Name" name="productName">
            <Input name="name" placeholder="Ex: Organic Apple..." />
          </Form.Item>
          <Form.Item label="Description" name="description">
            <Input name="description" placeholder="Ex: Juicy organic apples!" />
          </Form.Item>
          <Form.Item label="Category" name="category">
            <Select name="category" placeholder="Please select a category">
              <Option value="Fruit">Fruit</Option>
              <Option value="Vegetable">Vegetable</Option>
              <Option value="Poultry">Poultry</Option>
            </Select>
          </Form.Item>
          <Form.Item>
            <Button
              type="primary"
              htmlType="submit"
              onClick={this.handleFormSubmit}
            >
              Submit
            </Button>
          </Form.Item>
        </Form>
      </div>
    );
  }
}
ReactDOM.render(<ItemForm />, document.getElementById("container"));

答案 2 :(得分:0)

使用onChange对其进行管理,如下所示。

state = {
    status: ""
  };

          <Form.Item label="Status">
            <Select
              name="status"
              onChange={value => {
                this.setState({ status: value });
              }}
              placeholder="Please choose the status"
            >
              <Option value="new">New</Option>
              <Option value="open">Open</Option>
              <Option value="rejected">Rejected</Option>
              <Option value="deferred">Deferred</Option>
              <Option value="reopened">Reopened</Option>
            </Select>
          </Form.Item>

答案 3 :(得分:0)

与您期望的不同,在Ant Design Select中,您无法通过调用以下方法获取值:

onChange={(e)=>console.log(e.target.value)}

不。取而代之的是,您只需访问事件本身即可直接获得值,如下所示:

onChange={(e)=>console.log(e)}

答案 4 :(得分:0)

您可以按如下方式解决此问题。 (适用于 Antd)

 <Select
    onChange={(text, index) => {
    console.log(index.children);
    }}
 >