v-model结合checkbox
作者:互联网
单选
<body> <div id="app"> <label for="agree"> <input type="checkbox" v-model="agree" id="agree" >同意协议 </label> <button :disabled="!agree">下一步</button> {{agree}} </div> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script> new Vue({ el:'#app', data:{ agree: false } }) </script> </body>
多选
<body> <div id="app"> <input type="checkbox" value="篮球" v-model="hobbies">篮球 <input type="checkbox" value="乒乓球" v-model="hobbies">乒乓球 <input type="checkbox" value="羽毛球" v-model="hobbies">羽毛球 <input type="checkbox" value="台球" v-model="hobbies">台球 <br>您已选择:{{hobbies}} </div> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script> new Vue({ el:'#app', data:{ hobbies: [] } }) </script> </body>
多选(常用写法)
<body> <div id="app"> <label v-for="item in hobbies"> <input type="checkbox" :value="item" v-model="hobby">{{item}} </label> <br>您已选择:{{hobby}} </div> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script> new Vue({ el:'#app', data:{ hobbies: ['篮球','乒乓球','羽毛球','台球'],//后台返回 hobby:[] } }) </script> </body>
标签:el,checkbox,app,Vue,结合,hobbies,new,model,data 来源: https://www.cnblogs.com/ixtao/p/15912134.html