<div id="app">
<div> <input type="checkbox" v-model="all.check" @change="allCheck()">{{all.name}}</div>
<ul>
<li v-for="(list,index) in lists">
<input type="checkbox" v-model="list.check" @change="onCure()">
{{list.name}}
</li>
</ul>
</div>
<script>
new Vue({
el:'#app',
data:{
all:{name:'全選',check:false},
lists:[
{name:'北京1',check:false},
{name:'北京2',check:false},
{name:'北京3',check:false},
{name:'北京4',check:false},
{name:'北京5',check:false},
{name:'北京6',check:false},
]
},
methods:{
allCheck:function(){
this.lists.forEach(item=>{
item.check=this.all.check;
})
},
onCure:function(){
//第一種方法
// var select=this.lists.filter(function(v){
// return v.check;
// })
// console.log(select.length);
// if(select.length==this.lists.length){
// this.all.check=true;
// }else{
// this.all.check=false;
// }
// select.length==this.lists.length ? this.all.check=true : this.all.check=false; //三木運算寫法
//every()方法 :檢測數組所有元素是否符合指定的條件,如果有一個條件不滿足,就返回false,反之返回true
//some() : 檢測數組中只要有一個滿足條件,返回true,都不滿足時,返回true。
this.all.check = this.lists.every(function(item){
return item.check
});
}
}
})
var arr =[3,26,6,22];
var a = arr.every(function(item){ //some時返回true every時返回false
return item >3
});
console.log(a)
</script>