uniapp uview实现标签最多选中三个,超过三个提示,少于三个push进一个新数组

        <view style="padding:40rpx 20rpx;">
                    <u-grid :border="false" col="4">
                        <u-grid-item v-for="(item, index) in checkboxs2" :key="index" custom-style="opacity:1">
                            <view class="" style="margin:20rpx 10rpx;">
                                <u-tag :text="item.title" :plain="!item.checked" type="success" size="large" :name="index" @click="checkboxClick2($event, item)"></u-tag>
                            </view>
                        </u-grid-item>
                    </u-grid>
                </view>

checkboxs2: [
                {
                    title: '近地铁',
                    checked: false
                },
                {
                    title: '近轻轨',
                    checked: false
                },
                {
                    title: '交通便',
                    checked: false
                },
                {
                    title: '有商圈',
                    checked: false
                },
                {
                    title: '小吃多',
                    checked: false
                }
            ]

          // 判断选中的 checkbox 数量是否超过三个
          if (selectedCheckboxCount > 3) {
            // 禁用其他未选中的 checkbox
            this.checkboxs2.forEach((checkbox, index) => {
              if (!checkbox.checked && index !== event) {
                checkbox.disabled = true;
              }
            });
            // 不再将当前 checkbox 的 title 添加到数组中,并弹出提示框
            if (item.checked) {
              this.checkboxs2[event].checked = false;
              selectedCheckboxCount--;
                uni.$u.toast('最多只能选3个哦~');
            }
          } else {
            // 启用所有 checkbox
            this.checkboxs2.forEach(checkbox => {
              checkbox.disabled = false;
            });
            // 根据 checkbox 状态变化,将对应的 title 添加到或从数组中移除
            if (item.checked) {
              this.newArr2.push(item.title);
            } else {
              const titleIndex = this.newArr2.indexOf(item.title);
              if (titleIndex !== -1) {
                this.newArr2.splice(titleIndex, 1);
              }
            }
          }
          // 打印当前数组内容
          console.log(this.newArr2);
        }

效果图

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值