React设置select的默认值

时间:2018-03-20 11:56:00

标签: reactjs

这是我的 SearchForm.js 类,period是一个选择列表

import React from 'react';
import ReactDOM from 'react-dom';
import axios from 'axios';

class SearchForm extends React.Component {

        constructor(props) {
            super(props)

            this.state = {
                position: '',
                area: '',
                period: '',
                experience: {
                    type: Array,
                    default: () => []
                }
            }
            this.handlePeriodChange = this.handlePeriodChange.bind(this);
        }


        handlePeriodChange(e) {
            this.setState({
                [e.target.name]: e.target.value
            });
        }      

        render() {
            return ( <
                form className = 'form search-form'
                onSubmit = {
                    this.handleSubmit
                } >
                <
                div className = "form-row" >

                    <
                    div className = "form-group col-md-2" >
                    <
                    label htmlFor = "period" > Period *< /label> <
                    select className = "form-control"
                    name = "period"
                    id = "period"
                    onChange = {
                        this.handlePeriodChange
                    }
                    value = {
                        this.state.period
                    } >
                    <
                    option value = "1" > 1 < /option> <
                    option value = "3" > 3 < /option> <
                    option value = "7" > 7 < /option> <
                    option value = "30" > 30 < /option> < /
                    select > <
                    /div> < /
                    div >


                    <
                    div className = "form-row" >
                    <
                    div className = "form-group col-md-12 pt-3" >
                    <
                    input id = 'form-button'
                    className = 'btn btn-primary'
                    type = 'submit'
                    placeholder = 'Send' / >
                    <
                    /div> < /
                    div > <
                    /form>
                )
            }
        }

        export {
            SearchForm
        }

如何为期间select设置免责值?

1 个答案:

答案 0 :(得分:4)

为选择选项

提供状态的默认值
 this.state = {
            position: '',
            area: '',
            period: '1',
            experience: {
                type: Array,
                default: () => []
            }
        }
       ...
 <select 
      className = "form-control"
      name = "period"
      id = "period"
      onChange = {this.handlePeriodChange}
      value = {this.state.period
      }
 >
      <option value = "1" > 1 < /option> 
      <option value = "3" > 3 < /option> 
      <option value = "7" > 7 < /option> 
      <option value = "30" > 30 < /option>
 < /select >

或添加defaultValue属性以选择

 <select 
      className = "form-control"
      name = "period"
      id = "period"
      defaultValue="1"
      onChange = {this.handlePeriodChange}
      value = {this.state.period}
 >
      <option value = "1" > 1 < /option> 
      <option value = "3" > 3 < /option> 
      <option value = "7" > 7 < /option> 
      <option value = "30" > 30 < /option>
 < /select >