美文网首页
自定义/修改小程序radio和checkbox样式

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

作者: 银河星海 | 来源:发表于2018-09-09 13:07 被阅读645次

想把小程序radio和checkbox改成标签的样式,研究了一个早上,终于搞定,最终效果如下,样式可以随意修改。


自定义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: 20rpx;
  margin-top: 50rpx;
}

.ui-radio radio,.ui-radio checkbox {
  display: none;
}

.label{
  margin-left: 10rpx;
  display: flex;
  flex-direction: row;
  flex-wrap:wrap;
}

.ui-radio{
  border: 1px solid #D9D9D9;
  border-radius: 5px;
  width: 160rpx;
  height: 70rpx;
  text-align: center;
  margin: 10rpx 10rpx;
  white-space:nowrap;
  font-size: 14px;
}

.ui-radio.active{
  background-color: #1AAD19;
  color: #fff;
  border-radius: 5px;
  border: 1px solid #1AAD19;
  width: 160rpx;
  height: 70rpx;
  text-align: center;
  margin: 10rpx 10rpx;
  white-space:nowrap;
}

.text{
  color:#000;
  line-height:70rpx;
  font-size: 14px;
}

.ui-radio.active .text{
  color:#fff;
  line-height:70rpx;
  font-size: 14px;
}

参考文章:

小程序的radio样式不能自定义吗? (这个代码有个小bug,css里active写成checked了,改一下就行。)
微信小程序checkbox选中改变样式
微信小程序获取多选框选中值和选中值对应的id

9月9日

相关文章

网友评论

      本文标题:自定义/修改小程序radio和checkbox样式

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