简体   繁体   English

单选按钮在reactjs中不起作用

[英]Radio button not working in reactjs

<FormGroup controlId="gender">
  <ControlLabel>Gender</ControlLabel>
  <br />
  <Radio
    name="radioGroup"
    type="radio"
    inline
    value={this.state.gender === "male"}
  >
    onChange={this.handleChange}
    Male
  </Radio>
  <Radio
    name="radioGroup"
    type="radio"
    inline
    value={this.state.gender === "female"}
  >
    onChange={this.handleChange}
    Female
  </Radio>
</FormGroup>

code not working..unable to set the state using this code 代码不起作用..无法使用此代码设置状态

In your state define a gender object: 在您的状态中定义性别对象:

this.state = {
  gender: {
    male: 'male',
    female: 'female'
  }     
}

You set a boolean value in the radio 'value' field. 您可以在单选“值”字段中设置布尔值。 Should be just: value={this.state.gender.male} 应该只是:value = {this.state.gender.male}

And the onChange should be in your Radio tag too. 并且onChange也应该在您的Radio标签中。

<Radio name="radioGroup" 
       inline 
       value={this.state.gender.male}
       onChange={this.handleChange}
>
</Radio>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM