答案 0 :(得分:1)
您可能已经忘记了某些东西或未正确实现它,因为您没有提供我要附加的代码,但前提是您正在使用类组件...
import React from "react";
export default class RadioButtons extends React.Component {
constructor() {
super();
this.state = {
selectedOption: 'Male'
};
}
radioChange = (e) => {
this.setState({
selectedOption: e.currentTarget.value
});
}
render() {
return (
<div>
<input type="radio"
value="Male"
checked={this.state.selectedOption === "Male"}
onChange={this.radioChange} />Male
<input type="radio"
value="Female"
checked={this.state.selectedOption === "Female"}
onChange={this.radioChange}/>Female
<input type="radio"
value="Other"
checked={this.state.selectedOption === "Other"}
onChange={this.radioChange}/>Other
<h3>this.state.selectedOption: {this.state.selectedOption}</h3>
</div>
);
}
}
这是指向sandbox
的链接