react多选框
react多選框數組里,需要定義checked屬性。
import React, { Component } from 'react' class App extends Component {constructor(props) {super(props);this.state = { checkboxItems:[{content:"吃",checked:false},{content:"喝",checked:false},{content:"玩",checked:false},{content:"樂",checked:false},] }}render() { return ( <div>{this.state.checkboxItems.map((ele,index)=>{return (<span key={index}><input type="checkbox" name="" value={index} checked={ele.checked} onChange={()=>this.getVal(index)}/><span>{ele.content}</span></span> )})}</div> );}getVal = (index)=>{//復制原來的數組var items = [...this.state.checkboxItems]//checked取反items[index].checked =!items[index].checkedthis.setState({//更新checkboxItems全部選項 checkboxItems:items})} }export default App;?
轉載于:https://www.cnblogs.com/luguankun/p/11194938.html
總結
- 上一篇: CSS 常用命名
- 下一篇: SQL注入语法类型——报错注入