el-radio单选框,取消选中

1.背景:在公司开发需求中有一个选择颜色的单选框(黑色,白色),每种颜色选择后均支持取消选中,可是el-radio标签不支持取消选中。

方法1: 

<el-radio-group v-model="radioColor">
    <el-radio :label="'black'" @click.native.prevent="clickitemdataType('black')">黑色</el-radio>
    <el-radio :label="'white'" @click.native.prevent="clickitemdataType('white')">白色</el-radio>
</el-radio-group>
clickitemdataType (e) { // e为radio的label值
      e === this.radioColor ? this.radioColor = '' : this.radioColor = e
 },

方法2: 换checkbox,设置max=1即可

<el-checkbox-group v-model="checkList" :max="1">
    <el-checkbox label="A"></el-checkbox>
    <el-checkbox label="B"></el-checkbox>
</el-checkbox-group>

data () {
    return {
        checkList: ['A']
    }
}

 tips:

  1. 给vue绑定组件的时候,需要加一个native,不加的话会被认为是要监听item里面的自定义事件,

  2. 加prevent,是用来阻止默认事件的,相当于Jquery里面的event.preventDefault()方法阻止元素发生默认的行为。

猜你喜欢

转载自blog.csdn.net/weixin_43923808/article/details/131781999