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


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

    1. 你喜欢什么颜色?()
    2. A、红色
    3. B、紫色
    4. C、蓝色
    5. D、灰色
    6. E、均无
    7. 如该题,当选择选项E时,明显与其他选项互斥。这个时候经常会出现勾选E后,ABCD禁止选中的现象

    以下为效果图:

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

    具体代码:

    1. html:
    2. <checkbox-group v-model="selectedItems" ref="checkboxGroup" @change="changeCheckbox">
    3. <label class="options acea-row row-between row-middle" v-for="item in TestObject" :key="item.code">
    4. <text>{{item.name}}</text>
    5. <checkbox :value="item.code" :disabled="item.disabled" :checked="selectedItems.includes(item.code)"></checkbox>
    6. </label>
    7. </checkbox-group>
    8. JS:
    9. data:{
    10. selectedItems:[],
    11. TestObject: [
    12. { code: 'A', name: '红色', mutex: [] },
    13. { code: 'B', name: '紫色', mutex: [] },
    14. { code: 'C', name: '蓝色', mutex: [] },
    15. { code: 'D', name: '灰色', mutex: [] },
    16. { code: 'E', name: '均无', mutex: ['A','B','C','D'] }
    17. ]
    18. }
    19. ....
    20. changeCheckbox({detail}){
    21. const {TestObject,selectedItems} = this;
    22. const selectedValue = detail.value;
    23. TestObject.forEach(item => {item.disabled = false });
    24. selectedValue.forEach(value => {
    25. const option = TestObject.find(opt => opt.code === value);
    26. if (option && option.isMutex.length) {
    27. option.isMutex.forEach(mutexCode => {
    28. const mutexOption = TestObject.find(opt => opt.code === mutexCode);
    29. if (mutexOption) {
    30. mutexOption.disabled = true;
    31. const index = selectedValue.indexOf(mutexCode);
    32. if (index > -1) {
    33. selectedValue.splice(index, 1);
    34. }
    35. }
    36. });
    37. }
    38. });
    39. this.$set(this, 'selectedItems', selectedValue);
    40. }

  • 相关阅读:
    ATF源码篇(六):docs文件夹-Components组件(5)EL3
    devtools安装
    Android 12.0 Launcher3 去掉Hotseat功能
    Webpack5 快速入门
    趣玩行为商城:用智能消费行为开启财富新生活!
    制作一个简单HTML宠物猫网页(HTML+CSS)
    docker部署Jenkins
    Docker 简介 & 安装
    如何解决电脑出现msvcp140.dll丢失问题,msvcp140.dll丢失的最全解决方法
    Nginx
  • 原文地址:https://blog.csdn.net/qq_34458824/article/details/133991456