如果我打电话,如何获取单选按钮的值更新单选按钮在 RadioGroup 中,它会导致错误。我需要使用(react-radio-buttons)在控制台中打印为男性或女性。单选按钮打印正确,但我无法获取该值。先感谢您。
class CreateUserComponent extends React.Component {
constructor(props) {
super(props);
this.state={radio:''}
}
updateRadioButton(e) {
this.setState({ radio: e.target.value });
}
<RadioGroup horizontal>
<RadioButton value="Male">Male</RadioButton>
<RadioButton value="Female">Female</RadioButton>
</RadioGroup>
那么根据DOCS https://github.com/mu29/react-radio-buttons#usage这个库的RadioGroup
has a onChange
您可以传递的 prop 处理程序将返回选定的值,然后您可以将其设置在状态中或将其传递。
这是您的代码的小型运行示例:
debugger
const RadioGroup = ReactRadioButtonsGroup.ReactRadioButtonsGroup;
const RadioButton = ReactRadioButtonsGroup.ReactRadioButton;
class App extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
onRadiochange = value => {
console.log(value);
};
render() {
return (
<div>
<RadioGroup horizontal onChange={this.onRadiochange}>
<RadioButton value="Male">Male</RadioButton>
<RadioButton value="Female">Female</RadioButton>
</RadioGroup>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected] /cdn-cgi/l/email-protection/build/bundle.min.js"></script>
<div id="root"></div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)