简体   繁体   English

在reactJs中单击删除按钮时如何删除多个选定的复选框

[英]How to delete multi selected check-boxes when a delete button is clicked in reactJs

I have multiple check boxes and a single delete button at the top. 我在顶部有多个复选框和一个删除按钮。 After making the selection I should remove the selected check-boxes . 做出选择之后,我应该删除选中的复选框。 How can I do that in React Js . 我如何在React Js中做到这一点。

Try this: 尝试这个:

 class App extends React.Component { state = { list: [ { id: 1, label: 'Reading', value: false }, { id: 2, label: 'Playing game', value: false }, { id: 3, label: 'Listening to music', value: false }, { id: 4, label: 'Watching TV', value: true } ] }; handleChange = e => { const id = e.target.id; this.setState(prevState => { return { list: prevState.list.map( li => (li.id === +id ? { ...li, value: !li.value } : li) ) }; }); }; handleClick = () => { this.setState(prevState => { return { list: prevState.list.filter(li => !li.value) }; }); }; render() { return ( <div className="App"> {this.state.list.map(e => ( <div key={e.id}> <input type="checkbox" id={e.id} checked={e.value} onChange={this.handleChange} /> <label htmlFor={e.id}>{e.label}</label> </div> ))} <button onClick={this.handleClick}>Delete</button> </div> ); } } ReactDOM.render( < App / > , document.getElementById('root')) 
 <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> <div id="root"><div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM