React中处理radio单选按钮
2021-04-13 11:13:23
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Hello React</title>
<script src="https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"></script>
<script src="https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"></script>
<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
class App extends React.Component{
state = {
demo: ""
}
handerClick = (e)=>{
this.setState({demo: e.target.value})
}
componentDidMount() {
setTimeout(()=>{
this.setState({demo: 2})
},3000)
}
render(){
return (
<div>
吃饭<input type="radio" name="demo" value="1" checked={this.state.demo === "1"} onChange={this.handerClick} />
睡觉<input type="radio" name="demo" value="2" checked={this.state.demo === "2"} onChange={this.handerClick} />
打豆豆<input type="radio" name="demo" value="3" checked={this.state.demo === "3"} onChange={this.handerClick} />
</div>
)}
}
let el = <App />
let root = document.getElementById('root');
ReactDOM.render(el, root);
</script>
</body>
</html>
```