admin管理员组文章数量:1415100
Trying React radio but the strange thing is that the radio selection will not update its view. Can anyone take a look at the code below and let me know what's wrong?
class Test extends React.Component {
constructor(props){
super(props);
this.state = {
radioVal: "first"
}
}
handleRadioSelection(e){
e.preventDefault();
this.setState({
radioVal:e.target.value
})
}
render(){
return (
<div>
<label>
<input type="radio" onChange={this.handleRadioSelection.bind(this)}
checked={ this.state.radioVal==="first"}
value="first"
name="radio1" /> 1 <br />
</label>
<label>
<input type="radio" onChange={this.handleRadioSelection.bind(this)}
checked={this.state.radioVal==="second"}
value="second"
name="radio1" /> 2 <br />
</label>
{ "Selected: " } {this.state.radioVal}
</div>
)
}
}
React.render(<Test />, document.getElementById('container'));
Trying React radio but the strange thing is that the radio selection will not update its view. Can anyone take a look at the code below and let me know what's wrong?
class Test extends React.Component {
constructor(props){
super(props);
this.state = {
radioVal: "first"
}
}
handleRadioSelection(e){
e.preventDefault();
this.setState({
radioVal:e.target.value
})
}
render(){
return (
<div>
<label>
<input type="radio" onChange={this.handleRadioSelection.bind(this)}
checked={ this.state.radioVal==="first"}
value="first"
name="radio1" /> 1 <br />
</label>
<label>
<input type="radio" onChange={this.handleRadioSelection.bind(this)}
checked={this.state.radioVal==="second"}
value="second"
name="radio1" /> 2 <br />
</label>
{ "Selected: " } {this.state.radioVal}
</div>
)
}
}
React.render(<Test />, document.getElementById('container'));
Share
Improve this question
edited Jul 1, 2022 at 19:43
Jonas
129k103 gold badges328 silver badges405 bronze badges
asked Apr 15, 2017 at 18:54
max limax li
2,4674 gold badges31 silver badges46 bronze badges
1 Answer
Reset to default 5Remove e.preventDefault();
from handleRadioSelection
. Is there a reason why you added that line in the first place?
本文标签: javascriptReact radio button not update selectionStack Overflow
版权声明:本文标题:javascript - React radio button not update selection - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745185781a2646678.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论