当前位置:   article > 正文

uniapp h5项目实现多选按钮/多选标签/多选框

uniapp h5项目实现多选按钮/多选标签/多选框

需求:实现简单多选功能,遍历数据,添加样式

1.效果图 

2.以下代码粘贴到代码中,可直接运行,html代码

  1. <view class="page index">
  2. <view class="list-box">
  3. <view v-for="(item,index) in typeList" :key="index" @click="choice(index)"
  4. :class="[item.selected?'selde':'noselde']">
  5. {{item.selected?item.title:item.title}}
  6. </view>
  7. </view>
  8. <view class="valueList">
  9. {{selectList}}
  10. </view>
  11. </view>

3.js

  1. //给标签赋值
  2. typeList: [{
  3. selected: false,
  4. title: '综合类 20积分一公斤'
  5. },
  6. {
  7. selected: false,
  8. title: '金属类 20积分一公斤'
  9. },
  10. {
  11. selected: false,
  12. title: '纸类 20积分一公斤'
  13. },
  14. {
  15. selected: false,
  16. title: '纸类 20积分一公斤'
  17. },
  18. {
  19. selected: false,
  20. title: '纸类 20积分一公斤'
  21. }, {
  22. selected: false,
  23. title: '纸类 20积分一公斤'
  24. },
  25. {
  26. selected: false,
  27. title: '纸类 20积分一公斤'
  28. }
  29. ],
  30. selectId: [],
  31. //选择按钮
  32. choice(index) {
  33. console.log('index', index)
  34. //当再次被选中时,取消当前选中项
  35. if (this.typeList[index].selected == true) {
  36. this.typeList[index].selected = false;
  37. //取消选中时删除数组中的值
  38. for (var i = 0; i < this.selectId.length; i++) {
  39. if (this.selectId[i] === this.typeList[index].title) {
  40. this.selectId.splice(i, 1);
  41. }
  42. }
  43. this.selectList = this.selectId
  44. console.log("选中的值", this.selectId)
  45. } else {
  46. this.typeList[index].selected = true;
  47. this.selectId.push(this.typeList[index].title)
  48. for (var i = 0; i < this.selectId.length; i++) {
  49. console.log("选中的值", this.selectId[i])
  50. }
  51. this.selectList = this.selectId
  52. console.log("选中的值", this.selectId)
  53. }
  54. }

5.css 

  1. .list-box {
  2. display: flex;
  3. justify-content: space-between;
  4. flex-wrap: wrap;
  5. // padding: 16rpx;
  6. border-radius: 10rpx;
  7. view {
  8. width: 45%;
  9. height: 60upx;
  10. line-height: 60upx;
  11. text-align: center;
  12. margin-bottom: 30upx;
  13. // &:not(:nth-child(3n)) {
  14. // margin-right: calc(10% / 2);
  15. // }
  16. }
  17. }
  18. /* 已选择 */
  19. .selde {
  20. border: 1px solid #3EAAFB;
  21. background: #3EAAFB;
  22. color: #FFFFFF;
  23. border-radius: 20rpx;
  24. font-size: 20rpx;
  25. padding: 0 10rpx;
  26. }
  27. /* 未选择 */
  28. .noselde {
  29. border: 1px solid #959595;
  30. background: #FFFFFF;
  31. color: #959595;
  32. border-radius: 20rpx;
  33. font-size: 20rpx;
  34. padding: 0 10rpx;
  35. }
  36. .valueList {
  37. margin-top: 20rpx;
  38. padding: 20rpx;
  39. display: flex;
  40. justify-content: center;
  41. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/429243
推荐阅读
相关标签
  

闽ICP备14008679号