css:
.radioStyle { display:inline-block; width: .24rem; height: .24rem; background: url("img/radio_icon.png")0 -0.34rem no-repeat;background-size: 100%; margin-right: 0.2rem; } .radioStyleChecked { background: url("img/radio_icon.png")0 0 no-repeat; background-size: 100%; }
html:
<div class="app">
<div class="radio-box" v-for="(item,index) in radios" :key="item.id">
<span class="radioStyle" :class="{'radioStyleChecked':item.isChecked}" @click="huoqu(index)"></span>
<input v-model="sex" :value="item.value" class="input-radio" :checked='item.isChecked' type="radio" style="display: none">{{item.label}}
</div>
</div>
js:
<script> var vm = new Vue({ el:".app", data:{ 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; }, } }); </script>