(1)代码基于vue和elementui框架 el-checkobx
![1](https://i-blog.csdnimg.cn/blog_migrate/bd5a7fdb214415a58d83eb252a11514f.png)
![](https://i-blog.csdnimg.cn/blog_migrate/21b7f56e11a40035fb442c25b003ca7c.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ee0d497c68ea25c0e5379d00762d6cf8.png)
//html部分
<el-checkbox-group :value="templateOne" @input="templateOne = $event.slice(-1)">
<el-checkbox v-for="(cont,index) in templateList" :key="index" :label="cont">
<pre>{{cont.content}}</pre>
<p>{{cont.title}}</p>
</el-checkbox>
</el-checkbox-group>
data里面定义
templateOne:[],
templateList:[],
这里的templateList的值是我从后台获取的数据,赋值给templateList的
需要注意的是这里我没有 使用v-model绑定el-checkbox。原理就是每一次我选择的时候,将数组slice(-1),只留下最后一个.
el-checkbox中的:label后面跟的cont是传el-checkbox-group的 :value的templateOne
在vue的组建中能看到未选中的时候是空
任意选中以后就将选中的cont push进了templateOne中
(2)vue原生手写实现单选效果并能实现不选
<div @click="checkChanged(0)">
<span :class="[ is_promoted == false? 'chose' : 'chose-act']"></span>
<span>是否推广</span>
</div>
<div @click="checkChanged(1)">
<span :class="[ is_adv == false? 'chose' : 'chose-act']"></span>
<span>是否推广</span>
</div>
data(){
is_promoted:false,
is_adv:false
},
methods:{
checkChanged(type){
if(type == 0){
this.is_promoted =!this.is_promoted;
this.is_adv = false;
}else if( type == 1){
this.is_adv =!this.is_adv;
this.is_promoted = false;
}
}
}
<style>
.chose{
display: inline-block;
width: 20px;
height: 20px;
border: 1px solid #999;
background-color:#fff;
}
.chose-act{
display: inline-block;
width: 20px;
height: 20px;
border: 1px solid #999;
background-color: red;
}
</style>
效果图: