Radio buttons
<! DOCTYPEhtml>
<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>
Copy the code
Check box
<! DOCTYPEhtml>
<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){
let items = this.state.value.slice();
let index = items.indexOf(item);
index === -1 ? items.push(item) : items.splice(index, 1);
this.setState(({ tagIndexList }) = > ({
tagIndexList: tagIndexList.includes(num)
? tagIndexList.filter(item= >item ! == num) : [...tagIndexList, num],value: items
}));
console.log(items);
}
render() {
return (
<div>
{
this.state.arr.map((item, index) => {
return (
<div key={item.item} className="item">
<div className={this.state.tagIndexList.includes(index)?"checked":""} onClick={()= >this.handleClick(index,item.item)}></div><span>{item.item}</span>
</div>)})}</div>
)
}
}
ReactDOM.render(
<CheckBox />.document.getElementById('root'))</script>
</body>
</html>
Copy the code
Welcome to pay attention to my public account “front-end history robbery Road”
Reply keyword e-books, you can get nearly 12 front-end popular e-books.
Reply keywords little Red Book 4th edition, you can get the latest “JavaScript advanced Programming” (4th edition) ebook.
You can also add me to wechat. I have wooed many IT bigwigs and created a technical exchange and article sharing group. Welcome to join.
Author: Vam’s Golden Bean Road
Main area: front-end development
My wechat account is Maomin9761
Wechat public number: the front end of the road through robbery