【Kevin Learn 小程序】--> checkbox

这篇博客展示了如何在小程序中实现Checkbox组件,包括组件的效果图、相关属性介绍,并提供了从app.js到checkbox.wxml的完整代码示例。
摘要由CSDN通过智能技术生成

效果图

在这里插入图片描述

属性

参考:checkbox

代码

  1. app.js
//app.js
App({
   
  onLaunch: function () {
   
    console.log('App Launch')
  },
  onShow: function () {
   
    console.log('App Show')
  },
  onHide: function () {
   
    console.log('App Hide')
  },
  globalData: {
   
    hasLogin: false
  }
})
  1. app.json
{
   
  "pages": [
    "pages/checkbox/checkbox"
  ],
  "window": {
   
    "backgroundTextStyle": "light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "WeChat",
    "navigationBarTextStyle": "black"
  },
  "style": "v2",
  "sitemapLocation": "sitemap.json"
}
  1. checkbox.js
Page({
   
  data: {
   
    items: [
      {
    value: 'USA', name: '美国' },
      {
    value: 'CHN', name: '中国', checked: 'true' },
      {
    value: 'BRA', name: '巴西' },
      {
    value: 'JPN', name: '日本' },
      {
    value: 'ENG', name: '英国' },
      {
    value: 'FRA', name: '法国' }
    ]
  },
  checkboxChange: function (e) {
   
    console.log('checkbox发生change事件,携带value值为:', e)

    var items = this.data.items, values = e.detail.value;
    for (var i = 0, lenI = items.length; i < lenI; ++i) {
   
      items[i].checked = false;

      for (var j = 0, lenJ = values.length; j < lenJ; ++j) {
   
        if (items[i].value == values[j]) {
   
          items[i].checked = true;
          break
        }
      }
    }

    this.setData({
   
      items: items
    })
  }
})
  1. checkbox.json
{
   
  "navigationBarTitleText": "checkbox组件"
}
  1. checkbox.wxml
<!--pages/checkbox/checkbox.wxml-->
<view class="container">
  <view class="page-body">
    <view class="page-section page-section-gap">
      <label class="checkbox">
        <checkbox value="cb" checked="true"/>选中
      </label>
      <label class="checkbox">
        <checkbox value="cb" />未选中
      </label>
      <label class="checkbox">
        <checkbox value="cb" disabled="true"/>禁用
      </label>
    </view>

    <view class="page-section">
      <view class="weui-cells weui-cells_after-title">
        <checkbox-group bindchange="checkboxChange">
          <label class="weui-cell weui-check__label" wx:for="{
   {items}}" wx:key="{
   {item.value}}">
            <view class="weui-cell__hd">
              <checkbox value="{
   {item.value}}" checked="{
   {item.checked}}"/>
            </view>
            <view class="weui-cell__bd">{
   {
   item.name}}</view>
          </label>
        </checkbox-group>
      </view>
    </view>
  </view>
</view>

  1. checkbox.wxss
@import "../../common/lib/weui.wxss";
.checkbox{
   
  margin-right: 20rpx;
}
  1. weui.wxss
page {
   
  line-height: 1.6;
  font-family: -apple-system-font, "Helvetica Neue", sans-serif;
}
icon {
   
  vertical-align: middle;
}
.weui-cells {
   
  position: relative;
  margin-top: 1.17647059em;
  background-color: #FFFFFF;
  line-height: 1.41176471;
  font-size: 17px;
}
.weui-cells:before {
   
  content: " ";
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  height: 1px;
  border-top: 1rpx solid #D9D9D9;
  color: #D9D9D9;
}
.weui-cells:after {
   
  content: " ";
  position: absolute;
  left: 0;
  bottom: 0;
  right: 0;
  height: 1px;
  border-bottom: 1rpx solid #D9D9D9;
  color: #D9D9D9;
}
.weui-cells__title {
   
  margin-top: .77em;
  margin-bottom: .3em;
  padding-left: 15px;
  padding-right: 15px;
  color: #999999;
  font-size: 14px;
}
.weui-cells_after-title {
   
  margin-top: 0;
}
.weui-cells__tips {
   
  margin-top: .3em;
  color: #999999;
  padding-left: 15px;
  padding-right: 15px;
  font-size: 14px;
}
.weui-cell {
   
  padding: 10px 15px;
  position: relative;
  display: -webkit-box;
  display: -webkit-flex;
  display: flex;
  -webkit-box-align: center;
  -webkit-align-items: center;
          align-items: center;
}
.weui-cell:before {
   
  content: " ";
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  height: 1px;
  border-top: 1rpx solid #D9D9D9;
  color: #D9D9D9;
  left: 15px;
}
.weui-cell:first-child:before {
   
  display: none;
}
.weui-cell_active {
   
  background-color: #ECECEC;
}
.weui-cell_primary {
   
  -webkit-box-align: start;
  -webkit-align-items: flex-start;
          align-items: flex-start;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Kevin-Dev

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

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

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

打赏作者

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

抵扣说明:

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

余额充值