小程序radio和checkbox自定义修改样式

直接上代码记录一下。

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;
    }

自己做一些修改就可以用了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泡在网上的蜘蛛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值