单选框
代码语言:javascript复制<!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>
<style>
.item{
width: 20px;
height: 20px;
margin: 20px 0;
border: 1px solid #666;
}
.item>div{
height: 20px;
}
.checked{
background: red;
}
</style>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
class CheckBox extends React.Component {
constructor(props) {
super(props);
this.state = {
tagIndexList: [],
value:[],
index:null,
arr: [
{
item: "item1"
},
{
item: "item2"
},
{
item: "item3"
},
{
item: "item4"
}
]
};
this.handleClick = this.handleClick.bind(this);
}
handleClick(num,item){
this.setState({
index:num
})
console.log(item);
}
render() {
return (
<div>
{
this.state.arr.map((item, index) => {
return (
<div key={item.item} className="item">
<div className={this.state.index===index?"checked":""} onClick={()=>this.handleClick(index,item.item)}></div><span>{item.item}</span>
</div>
)
})
}
</div>
)
}
}
ReactDOM.render(
<CheckBox />,
document.getElementById('root')
)
</script>
</body>
</html>
复选框
代码语言:javascript复制<!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>
<style>
.item{
width: 20px;
height: 20px;
margin: 20px 0;
border: 1px solid #666;
}
.item>div{
height: 20px;
}
.checked{
background: red;
}
</style>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/119882555