美文网首页
vue 自定义样式radio单选框 样式美化

vue 自定义样式radio单选框 样式美化

作者: 木西_816c | 来源:发表于2019-04-30 14:39 被阅读0次

1、先上图

image.png

2、template


<div class="radio-box" v-for="(item,index) in radios" :key="item.id">
    <span class="radio" :class="{'on':item.isChecked}"></span>
    <input  v-model="sex" :value="item.value" class="input-radio" :checked='item.isChecked'  @click="huoqu(index)" type="radio">{{item.label}}
</div>

3、script

data() {
      return {
        sex: '男',
        radios:[
          {
            label: '男',
            value:'男',
            isChecked: false,
          },
          {
            label: '女',
            value:'女',
            isChecked: false,
          }
        ]
  }
},
methods:{
  huoqu(index){
        // 先取消所有选中项
        this.radios.forEach((item) => {
          item.isChecked = false;
        });
        //再设置当前点击项选中
        this.sex = this.radios[index].value;
        // 设置值,以供传递
        this.radios[index].isChecked = true;
      },
}

相关文章

网友评论

      本文标题:vue 自定义样式radio单选框 样式美化

      本文链接:https://www.haomeiwen.com/subject/tzhunqtx.html