<template>
<div>
<button @click="checkAnti">反选</button>
<button @click="checkAll">全选</button>
<button @click="checkNone">全不选</button>
<input type="checkbox" value="" v-model="inputAll" @click="checkInputAll">
喜好:
<div v-for="item in inputArr">
{{item.text}} : <input type="checkbox" value="" v-model="item.checked">
</div>
{{isCheckItemAll}}
</div>
</template>
<script>
export default {
name: "index",
components:{},
data(){
return {
inputAll:false,
inputArr: [
{ text: '足球', checked: true },
{ text: '篮球', checked: false },
{ text: '羽毛球', checked: false },
{ text: '游泳', checked: false },
]
}
},
watch: {
},
mounted(){
},
destroyed() {},
activated() {},
methods: {
checkNone() {
this.inputArr.forEach(item => {
item.checked = false;
});
this.inputItemAll = false;
},
checkAll() {
this.inputArr.forEach(item => {
item.checked = true;
});
this.inputItemAll = true;
},
checkAnti() {
this.inputArr.forEach(item => {
item.checked = !item.checked;
});
},
checkInputAll(){
this.inputAll=!this.inputAll;
if(this.inputAll){
this.checkAll()
}else{
this.checkNone()
}
},
},
computed:{
isCheckItemAll(){
return this.inputArr.every((item)=>{
return item.checked === true ? this.inputAll = true : this.inputAll = false
})
}
},
}
</script>
<style scoped lang="less">
</style>