vue CheckBox全选和反选

通过一段时间vue学习。自己模拟练习了CheckBox的全选的反选 希望对大家有用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>helloworld</title>
    <script type="text/javascript" src='../assets/js/vue.js'></script>
</head>
<body>
    <h1>helloworld 实例</h1>
    <hr>
    <div id="app">
        <label><input type="checkbox"  v-model='isok' v-on:click='demo'/> 全选/反选</label>
       <ul>
            <li v-for="item in items">
                <input type="checkbox" :value='item' v-model='itemsd' @change='demo2'/>
            </li>
       </ul>
       <p>{{ itemsd }}</p>
    </div>
    <script type="text/javascript">
        var app=new Vue({
            el:'#app',
            data:{
               items:[5,6,7,8,9,1,2],  
               itemsd:[],
               isok:false  //判断CheckBox是否选中
            },
            methods:{
                demo:function(){   //全选/反选
                    debugger
                    if( !this.isok){ //全选中 所有CheckBox
                        this.items.forEach(function (fruit) {
                            debugger;
                            this.itemsd.push(fruit);
                        }, this);
                    }else{   //反选清楚所有CheckBox选中
                      this.itemsd=[]
                    }
                },
                demo2:function(){
                    debugger;
                    if(this.items.length==this.itemsd.length){ //判断每一个CheckBox是否选中   全选中让全选反选按钮选中
                        this.isok=true;
                    }else{  // 不选中 让全选反选按钮不选中
                        this.isok=false;
                    }
                }
            }
        })
    
    </script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值