el-checkbox遇到的问题

在官网中有实例

<template>
  <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
  <div style="margin: 15px 0;"></div>
  <el-checkbox-group v-model="checkedCities" @change="handleCheckedCitiesChange">
    <el-checkbox v-for="city in cities" :label="city" :key="city">{{city}}</el-checkbox>
  </el-checkbox-group>
</template>
<script>
  const cityOptions = ['上海', '北京', '广州', '深圳'];
  export default {
    data() {
      return {
        checkAll: false,
        checkedCities: ['上海', '北京'],
        cities: cityOptions,
        isIndeterminate: true
      };
    },
    methods: {
      handleCheckAllChange(val) {
        this.checkedCities = val ? cityOptions : [];
        this.isIndeterminate = false;
      },
      handleCheckedCitiesChange(value) {
        let checkedCount = value.length;
        this.checkAll = checkedCount === this.cities.length;
        this.isIndeterminate = checkedCount > 0 && checkedCount < this.cities.length;
      }
    }
  };
</script>

一般都是把el-checkbox放在el-checkbox-group里面进行循环的。

1:只能写@change事件而不能写@click。而且可以将这个事件加在el-checkbox-group上面。这样每次点击checkbox框变化它都能捕捉到。也可以放在el-checkbox上面这样点击时获取的是你当时点击的checkbox。

2:v-model上面的值是你checkbox的选中的值。v-model写在了checkbox-group上面,这样获取的就是循环的里面所有选中的,不是一条数组选中的。有了v-model就可以不用写:checked属性来决定他是否选中了。

遇到的问题:我在el-checkbox-group和el-checkbox都添加了@change事件,导致当我选中某一项值,el-checkbox的v-model的值没有变化。可能是无法判断浏览器到底执行了哪个事件。

解决:去掉el-checkbox-group

 <div class="lesson-publish-checkbox">
   <!--<el-checkbox-group v-model="checkedNumber[index]" @change="handleCheckedNumberChange($event,index)">-->
     <el-checkbox v-for="student in cls.students" :key="student.studentId" :label="student.studentId"
                  -model="student.isPublished" :disabled="student.hasPublished" @change="studentCheckChange(cls)">{{student.studentName}}
     </el-checkbox>
   <!--</el-checkbox-group>-->
 </div>
原文地址:https://www.cnblogs.com/shenting/p/10432439.html