Vue/uniapp 点击盒子二选一,多选

文章展示了如何在Vue.js应用中创建一个二选一的选择框和一个多选功能的盒子。通过绑定样式和事件处理函数,实现点击时切换样式和管理选中项。使用JavaScript的数组方法进行选中状态的跟踪与更新,限制最多可选三门学科。
摘要由CSDN通过智能技术生成

盒子二选一

 <view class="" style="
                          display: inline-block;
                          margin: 20rpx;
                          border: 1px solid gray;
                          font-size: 24rpx;
                          width: 120rpx;
                          height: 48rpx;
                          text-align: center;
                          line-height: 48rpx;
                          border-radius: 24rpx 24rpx 24rpx;
                          color: gray;
                        " v-for="(item, index) in subjectsList" :key="item"
                        :class="isNew === index ? 'customStyle' : ''"
                        @click="clicksubjectsBtn(index, item)">
                        {{ item }}
                      </view>
JS
 // 文理科
      clicksubjectsBtn(index, item) {
        this.isNew = index;
      },
CSS
 .customStyle {
    color: #fca531 !important;
    border: 1rpx solid #fca531 !important;
  }

盒子多选

<view class="" style="
                           display: inline-block;
                           margin: 20rpx;
                           border: 1px solid gray;
                           font-size: 24rpx;
                           width: 120rpx;
                           height: 48rpx;
                           text-align: center;
                           line-height: 48rpx;
                           border-radius: 24rpx 24rpx 24rpx;
                           color: gray;
                         " v-for="(item, index) in subjectsList" :key="item" :class="{
                          customStyle: rSelect.indexOf(index) != -1,
                        }" @click="clicksubjectsBtnHe(index, item)">
                        {{ item }}
                      </view>
JS
定义变量

rSelect: [],
  clicksubjectsBtnHe(index, item) {
        console.log(this.rSelect.indexOf(index) == -1); //true false
        if (this.rSelect.indexOf(index) == -1) {
          console.log(index, item) //打印下标
          if (this.rSelect.length < 3) {
            this.rSelect.push(index); //选中添加到数组里
            // 综合类选三存次选字段
            this.rSelectItem.push(item)
          } else {
            this.$refs.uToast.show({
              title: '任选三门学科',
              type: 'warning',
            })
          };
        } else {
          this.rSelect.splice(this.rSelect.indexOf(index), 1); //取消
          // 综合类选三存次选字段
          this.rSelectItem.splice(this.rSelectItem.indexOf(item), 1)
        }
        console.log(this.rSelectItem);
      },
CSS
 .customStyle {
    color: #fca531 !important;
    border: 1rpx solid #fca531 !important;
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值