【发布时间】:2019-11-23 13:54:30
【问题描述】:
我创建了带有 3 个单选按钮的单选按钮: 我想将石灰作为默认选中单选按钮,我将石灰设置为默认值但它不起作用。
这是我的代码,我不知道如何解决我的问题。
import React, {Component} from 'react';
class App extends Component{
constructor(props){
super(props)
this.handleflavorSubmit = this.handleflavorSubmit.bind(this)
this.onChangeRadio = this.onChangeRadio.bind(this)
this.state = {value : 'lime'};
}
onChangeRadio(e){
this.setState({value : e.target.value})
}
handleflavorSubmit(e){
alert("your favorite flavor is " + this.state.value)
e.preventDefault();
}
render(){
return(
<div>
<h1>Choose your flavor:</h1>
<form onSubmit = {this.handleflavorSubmit}>
<input type="radio" checked = {this.state.value === 'grapefruit'} onChange = {this.onChangeRadio} value= "grapefruit"/>Grapefruit
<input type = "radio" checked = {this.state.value === 'lime'} onChange = {this.onChangeRadio} value = "lime"/>Lime
<input type = "radio" checked = {this.state.value === 'orange'} onChange = {this.onChangeRadio} value = "orange"/>Orange
<input type = "submit" value = "submit"/>
</form>
</div>
)
}
}
export default App
【问题讨论】:
标签: javascript html reactjs jsx