JS多选答题时,选项互斥时的情况

在做答题类的项目时,应该会比较常见多选题选相互斥的问题,例如:

你喜欢什么颜色?()

A、红色

B、紫色

C、蓝色

D、灰色

E、均无


如该题,当选择选项E时,明显与其他选项互斥。这个时候经常会出现勾选E后,A、B、C、D禁止选中的现象

以下为效果图:

具体思路如下:在遍历展示完数据之后,首先我们要给所有的选项增加一个是否禁止使用的标识。当用户点击选项时判断当前项是否与其他选项互斥,如果互斥,除选中项之外的其他选项禁止使用即可。

具体代码:

html:

   <checkbox-group v-model="selectedItems" ref="checkboxGroup" @change="changeCheckbox">
       <label class="options acea-row row-between row-middle"  v-for="item in TestObject" :key="item.code">
        <text>{{item.name}}</text>
        <checkbox :value="item.code" :disabled="item.disabled" :checked="selectedItems.includes(item.code)"></checkbox>
      </label>
</checkbox-group>



JS:
data:{
   selectedItems:[],
   TestObject: [
        { code: 'A', name: '红色', mutex: [] },
        { code: 'B', name: '紫色', mutex: [] },
        { code: 'C', name: '蓝色', mutex: [] },
        { code: 'D', name: '灰色', mutex: [] },
        { code: 'E', name: '均无', mutex: ['A','B','C','D'] }
   ]
}




....


changeCheckbox({detail}){
   const {TestObject,selectedItems} = this;
   const selectedValue = detail.value;
   TestObject.forEach(item => {item.disabled = false  });
   selectedValue.forEach(value => {
        const option = TestObject.find(opt => opt.code === value);
        if (option && option.isMutex.length) {
            option.isMutex.forEach(mutexCode => {
            const mutexOption = TestObject.find(opt => opt.code === mutexCode);
            if (mutexOption) {
                 mutexOption.disabled = true;
                 const index = selectedValue.indexOf(mutexCode);
                 if (index > -1) {
                    selectedValue.splice(index, 1);
                 }
            }
          });
        }
   });
  this.$set(this, 'selectedItems', selectedValue);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值