<checkbox-group @change="selectUniverSity" checked>
<checkbox v-for="(item,index) in universityTypes"
class="m-2"
:key="item.typeId"
:value="item.typeId"
<!--每次触发一次多选则向checkUnType追加一次,使用includes判断如果当前typeId包含在此数组则为true从而控制选中,否则取消选中-->
:checked="checkUnType.includes(String(item.typeId))">
{{item.name}}
</checkbox>
</checkbox-group>
data(){
return{
universityList:[
{typeId:'',name:'不限' },
{typeId:'12',name:'哈呵呵'},
{typeId:'13',name:'哈嘿嘿'},
{typeId:'14',name:'哈呼呼'},
{typeId:'15',name:'哈拉拉'}
],
checkUnType:[]
}
},
methods:{
//我的需求是点击当前多选框中的不限,其他选框取消选中,点击其他选框不限选框取消选择
selectUniverSity(val){
let zhi=false;
//(2)但是选择非不限内容时需首先判断之前checkUnType的值是否数组长度为1且为空
if(this.checkUnType.length===1&&this.checkUnType.includes('')){
zhi=true;
}
this.checkUnType=[];
this.checkUnType=val.detail.value;
//(3)如果是则设置一个参数为true,将多选框中为空的一项删掉
if(zhi){
this.checkUnType.splice(0,1);
}else if(val.detail.value.includes('')){
//(1)首先判断如果所选的checkUnType包含''这一项则设置只包含'',去掉其它
this.checkUnType=[''];
}
//(4)最后接收获取的值赋给条件
if(this.checkUnType.length>0&&this.checkUnType[0]!==''){
this.universityList=this.checkUnType;
}
},
}