gpt4 book ai didi

reactjs - 如何获取Reactjs中单选按钮(react-radio-buttons)的值?

转载 作者:行者123 更新时间:2023-12-03 13:58:44 26 4
gpt4 key购买 nike

如何获取单选按钮的值,如果我在RadioGroup中调用updateRadioButton,会导致错误。我需要使用(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在此库中,RadioGroup 有一个 onChange 属性处理程序,您可以传递该处理程序,该处理程序将返回选定的值,然后您可以将其设置在状态中或将其传递。

这是您的代码的小型运行示例:

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/react-radio-buttons-group@1.0.2/build/bundle.min.js"></script>
<div id="root"></div>

关于reactjs - 如何获取Reactjs中单选按钮(react-radio-buttons)的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46729044/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com