直接上代码记录一下。
wxml代码:
<view class='title'>radio单选标签</view>
<radio-group bindchange="radioChange">
<view class='label'>
<label class="ui-radio {{item.checked==true?'active':''}}" wx:for="{{items}}">
<radio value="{{item.value}}" checked="{{item.checked}}" />
<text class="text">{{item.name}}</text>
</label>
</view>
</radio-group>
<view class='title'>checkbox多选标签</view>
<checkbox-group bindchange="checkboxChange">
<view class='label'>
<label class="ui-radio {{item.checked==true?'active':''}}" wx:for="{{checkboxItems}}">
<checkbox value="{{item.name}},{{item.id}}" checked="{{item.checked}}" />
<text class="text">{{item.name}}</text>
</label>
</view>
</checkbox-group>
js代码:
Page({
data: {
items: [
{ value: 'USA', name: '美国' },
{ value: 'CHN', name: '中国' },
{ value: 'BRA', name: '巴西' },
{ value: 'JPN', name: '日本' },
{ value: 'ENG', name: '英国' },
{ value: 'FRA', name: '法国' },
],
checkboxItems: [
{ value: 'USA', name: '美国', id: 0 },
{ value: 'CHN', name: '中国', id: 1 },
{ value: 'BRA', name: '巴西', id: 2 },
{ value: 'JPN', name: '日本', id: 3 },
{ value: 'ENG', name: '英国', id: 4 },
{ value: 'FRA', name: '法国', id: 5 },
]
},
radioChange: function (e) {
var items = this.data.items;
for (var i = 0; i < items.length; ++i) {
items[i].checked = items[i].value == e.detail.value
}
console.log(items)
this.setData({
items: items
});
},
checkboxChange: function (event) {
var items = this.data.checkboxItems;
var id = [];
//获取标签id
for (var i = 0; i < event.detail.value.length; i++) {
var idNum = event.detail.value[i].split(',');
id = id.concat(idNum[1])
}
for (var y = 0; y < items.length; y++) {
if (id.indexOf(y + "") != -1) {
items[y].checked = true;
} else {
items[y].checked = false;
}
}
this.setData({
checkboxItems: items
});
}
})
wxss代码:
.title{
margin-left: 10px;
margin-top: 25px;
}
.ui-radio radio, .ui-radio checkbox {
display: none;
}
.label {
margin-left: 5px;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
.ui-radio {
border: 1px solid #D9D9D9;
border-radius: 5px;
width: 80px;
height: 35px;
text-align: center;
margin: 5px 5px;
white-space: nowrap;
font-size: 14px;
}
.ui-radio.active {
background-color: #1AAD19;
color: #fff;
border-radius: 5px;
border: 1px solid #1AAD19;
width: 80px;
height: 35px;
text-align: center;
margin: 5px 5px;
white-space: nowrap;
}
.text {
color: #000;
line-height: 35px;
font-size: 14px;
}
.ui-radio.active .text {
color: #fff;
line-height: 35px;
font-size: 14px;
}
自己做一些修改就可以用了。