选择输入字段始终返回未定义

时间:2019-07-10 06:52:46

标签: javascript reactjs semantic-ui-react

当我尝试在输入字段中选择一个选项时,它必须将状态值设置为选定的选项,但是它返回未定义的

我正在使用语义ui react Form来接受输入,但是每当我选择该选项并提交它时,我就无法定义

 import React from 'react'
 import { Form, Input, TextArea, Button, Select, Container } from 
 'semantic-ui-react'

const RankOptions = [
{ key: 'lg', text: 'Lt-Gen', value: 'Lt-Gen' },
{ key: 'mg', text: 'Mj-Gen', value: 'Mj-Gen' },
{ key: 'b', text: 'Brig', value: 'Brig' },
{ key: 'col', text: 'Col', value: 'Col' },
{ key: 'lc', text: 'Lt-Col', value: 'Lt-Col' },
{ key: 'm', text: 'Major', value: 'Mj' },
{ key: 'capt', text: 'Capt', value: 'Capt' },
{ key: 'lt', text: 'Lt', value: 'Lt' },
{ key: '2lt', text: '2-Lt', value: 'Lt-2' },

 ]

  export default class Employee extends React.Component{
  state={}

    handleSubmit = () => {
    console.log(this.state)
    }
    handlerankChange = (e) => {
    const value = e.target.value
    this.setState({
     rank : value
    })
    }
    render() {
    return (
        <Container>
        <Form size='huge'>
            <Form.Group widths='equal'>
                <Form.Field 
                name = 'rank'
                control = {Select}
                label = 'Rank'
                options = {RankOptions}
                placeholder = 'Rank'
                value = {this.state.value}
                onChange = {this.handlerankChange}
                />

          <Button primary onClick= 
        {this.handleSubmit}>Submit</Button>
        </Form>
          </Container>
        )
        }

         }

状态必须是行列中的任何选项

3 个答案:

答案 0 :(得分:0)

rankstate的初始值设置为

state = {
  rank:''
}

并更改

           <Form.Field 
                name = 'rank'
                control = {Select}
                label = 'Rank'
                options = {RankOptions}
                placeholder = 'Rank'
                value = {this.state.rank}
                onChange = {this.handlerankChange}
                />

答案 1 :(得分:0)

有效的代码将为您提供帮助:

import React from "react";
import { render } from "react-dom";
import Hello from "./Hello";
import {
  Button,
  Form,
  Grid,
  Header,
  Image,
  Message,
  Segment,
  Label,
  Dropdown
} from "semantic-ui-react";
import Select from "react-select";
import "./index.css";

const styles = {
  fontFamily: "sans-serif",
  textAlign: "center"
};

class App extends React.Component {
  state = {
    selectedOption: ""
  };
  handleChange = selectedOption => {
    this.setState({ selectedOption });
  };

  render() {
    const { selectedOption } = this.state;
    const value = selectedOption && selectedOption.value;

    return (
      <div className="login-form">
        <Grid
          textAlign="center"
          style={{ height: "100%" }}
          verticalAlign="middle"
        >
          <Grid.Column style={{ maxWidth: 450 }} textAlign="left">
            <Form size="large">
              <Segment>
                <div>
                  <Select
                    name="form-field-name"
                    value={value}
                    onChange={this.handleChange}
                    options={[
                      { value: "one", label: "One" },
                      { value: "two", label: "Two" }
                    ]}
                  />
                </div>
              </Segment>
            </Form>
          </Grid.Column>
        </Grid>
      </div>
    );
  }
}

render(<App />, document.getElementById("root"));

codesandbox

上的代码源

答案 2 :(得分:0)

您无需通过e.target.value访问该值,回调为对象提供了键“值”;

例如:

import React from 'react'
     import { Form, Input, TextArea, Button, Select, Container } from 
     'semantic-ui-react'

const RankOptions = [
{ key: 'lg', text: 'Lt-Gen', value: 'Lt-Gen' },
{ key: 'mg', text: 'Mj-Gen', value: 'Mj-Gen' },
{ key: 'b', text: 'Brig', value: 'Brig' },
{ key: 'col', text: 'Col', value: 'Col' },
{ key: 'lc', text: 'Lt-Col', value: 'Lt-Col' },
{ key: 'm', text: 'Major', value: 'Mj' },
{ key: 'capt', text: 'Capt', value: 'Capt' },
{ key: 'lt', text: 'Lt', value: 'Lt' },
{ key: '2lt', text: '2-Lt', value: 'Lt-2' },

 ]

  export default class Employee extends React.Component{
  state={}

    handleSubmit = () => {
    console.log(this.state)
    }

    handlerankChange = ({ value }) => {
      this.setState({
        rank : value
      })
    }
    render() {
    return (
        <Container>
          <Form size='huge'>
              <Form.Group widths='equal'>
                  <Form.Field 
                  name = 'rank'
                  control = {Select}
                  label = 'Rank'
                  options = {RankOptions}
                  placeholder = 'Rank'
                  value = {this.state.rank} // this should be rank
                  onChange = {this.handlerankChange}
                  />
            </Form.Group>.

            <Button primary onClick= 
             {this.handleSubmit}>Submit</Button>
          </Form>
        </Container>
      )
    }

         }

希望这会有所帮助!