Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
11
rated 0 times [  18] [ 7]  / answers: 1 / hits: 46257  / 5 Years ago, mon, april 22, 2019, 12:00:00

I want to retrieve the value from my selection so I can make post requests. I have no problem getting from text input, but for some reason I can't get it from the drop down menu select. I end up getting a
TypeError: Cannot read property 'value' of undefined



Here is the code I am using.



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;

More From » html

 Answers
39

Use onChange which is fired when the value of the select changes. 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


[#52209] Tuesday, April 16, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
calicinthias

Total Points: 447
Total Questions: 101
Total Answers: 118

Location: Botswana
Member since Sat, Dec 31, 2022
1 Year ago
calicinthias questions
Sun, Jan 2, 22, 00:00, 2 Years ago
Wed, Jan 13, 21, 00:00, 3 Years ago
Mon, Aug 10, 20, 00:00, 4 Years ago
;