image.png

代码

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vue.js-表单-实战-全选与取消全选</title>
    <script src="//static.gzj2001.com/vue/vue.js"></script>
</head>
<body>
    <div id="app">
        <input type="checkbox" id="chkbox" value="" v-model="checked" v-on:change="call()">
        <label for="chkbox">全选</label>
        <hr>
        <input type="checkbox" name="" id="baidu" value="baidu" v-model="jieguo">
        <label for="baidu">baidu</label>

        <input type="checkbox" name="" id="taobao" value="taoboa" v-model="jieguo">
        <label for="taobao">taobao</label>

        <input type="checkbox" name="" id="qq" value="qq" v-model="jieguo">
        <label for="qq">qq</label>
        <hr>
        {{jieguo}}

    </div>
    <script>
        new Vue({
            el:"#app",
            data:{
                checked:false,
                jieguo:[],
                shuzu:["baidu","taobao","qq"]
            },
            methods:{
                call:function(){
                    if(this.checked){
                        this.jieguo= this.shuzu;
                    }else{
                        this.jieguo=[];
                    }
                }
            },
            watch:{
                "jieguo":function(){
                    if(this.jieguo.length == this.shuzu.length){
                        this.checked =true;
                    }else{
                        this.checked =false;
                    }
                }
            }
        })
    </script>
</body>
</html>