您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

ReactJS中表单的单选多选与反选的实现方法

2024/4/18 23:44:43发布21次查看
本篇文章主要介绍了reactjs实现表单的单选多选和反选的示例,非常具有实用价值,需要的朋友可以参考下
本文介绍了reactjs实现表单的单选多选和反选的示例,分享给大家,希望对大家有所帮助。
需求是对列表实现单选,反选和多选,全部清除的操作
...... this.state = { //初始化空数组,表示已经选择的 selectedstores:[], } ...... handleclick(e){ const newselection = e.target.value;//拿到点击的具体一项 let newselectionarray;//新建一个空数组 //判断点击项是否为选择状态,是的话清除选中状态 if(this.state.selectedstores.indexof(newselection) > -1) { newselectionarray = this.state.selectedstores.filter((s:any) => s !== newselection) } else { //不是的话就加入新选择数组 newselectionarray = [...this.state.selectedstores, newselection]; } this.setstate({ // 新选择数组统一改为选中状态 selectedstores: newselectionarray }); }
array.prototype.indexof()方法返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回-1。
语法:
arr.indexof(searchelement) arr.indexof(searchelement[, fromindex = 0])
array.prototype.filter()方法创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。
语法:
var new_array = arr.filter(callback[, thisarg])
以上就是reactjs中表单的单选多选与反选的实现方法的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product