label

用来改进表单组件的可用性,使用for属性找到对应的id,或者将控件放在该标签下,当点击时,就会触发对应的控件。

for优先级高于内部控件,内部有多个控件的时候默认触发第一个控件。

目前可以绑定的控件有:<button/>, <checkbox/>, <radio/>, <switch/>

属性名 类型 说明
for String 绑定控件的 id

示例代码:

在开发者工具中预览效果

  1. <view class="section section_gap">
  2. <view class="section__title">表单组件在label内</view>
  3. <checkbox-group class="group" bindchange="checkboxChange">
  4. <view class="label-1" wx:for="{{checkboxItems}}">
  5. <label>
  6. <checkbox hidden value="{{item.name}}" checked="{{item.checked}}"></checkbox>
  7. <view class="label-1__icon">
  8. <view class="label-1__icon-checked" style="opacity:{{item.checked ? 1: 0}}"></view>
  9. </view>
  10. <text class="label-1__text">{{item.value}}</text>
  11. </label>
  12. </view>
  13. </checkbox-group>
  14. </view>
  15. <view class="section section_gap">
  16. <view class="section__title">label用for标识表单组件</view>
  17. <radio-group class="group" bindchange="radioChange">
  18. <view class="label-2" wx:for="{{radioItems}}">
  19. <radio id="{{item.name}}" hidden value="{{item.name}}" checked="{{item.checked}}"></radio>
  20. <view class="label-2__icon">
  21. <view class="label-2__icon-checked" style="opacity:{{item.checked ? 1: 0}}"></view>
  22. </view>
  23. <label class="label-2__text" for="{{item.name}}"><text>{{item.name}}</text></label>
  24. </view>
  25. </radio-group>
  26. </view>
  1. Page({
  2. data: {
  3. checkboxItems: [
  4. {name: 'USA', value: '美国'},
  5. {name: 'CHN', value: '中国', checked: 'true'},
  6. {name: 'BRA', value: '巴西'},
  7. {name: 'JPN', value: '日本', checked: 'true'},
  8. {name: 'ENG', value: '英国'},
  9. {name: 'TUR', value: '法国'},
  10. ],
  11. radioItems: [
  12. {name: 'USA', value: '美国'},
  13. {name: 'CHN', value: '中国', checked: 'true'},
  14. {name: 'BRA', value: '巴西'},
  15. {name: 'JPN', value: '日本'},
  16. {name: 'ENG', value: '英国'},
  17. {name: 'TUR', value: '法国'},
  18. ],
  19. hidden: false
  20. },
  21. checkboxChange: function(e) {
  22. var checked = e.detail.value
  23. var changed = {}
  24. for (var i = 0; i < this.data.checkboxItems.length; i ++) {
  25. if (checked.indexOf(this.data.checkboxItems[i].name) !== -1) {
  26. changed['checkboxItems['+i+'].checked'] = true
  27. } else {
  28. changed['checkboxItems['+i+'].checked'] = false
  29. }
  30. }
  31. this.setData(changed)
  32. },
  33. radioChange: function(e) {
  34. var checked = e.detail.value
  35. var changed = {}
  36. for (var i = 0; i < this.data.radioItems.length; i ++) {
  37. if (checked.indexOf(this.data.radioItems[i].name) !== -1) {
  38. changed['radioItems['+i+'].checked'] = true
  39. } else {
  40. changed['radioItems['+i+'].checked'] = false
  41. }
  42. }
  43. this.setData(changed)
  44. }
  45. })
  1. .label-1, .label-2{
  2. margin-bottom: 15px;
  3. }
  4. .label-1__text, .label-2__text {
  5. display: inline-block;
  6. vertical-align: middle;
  7. }
  8. .label-1__icon {
  9. position: relative;
  10. margin-right: 10px;
  11. display: inline-block;
  12. vertical-align: middle;
  13. width: 18px;
  14. height: 18px;
  15. background: #fcfff4;
  16. }
  17. .label-1__icon-checked {
  18. position: absolute;
  19. top: 3px;
  20. left: 3px;
  21. width: 12px;
  22. height: 12px;
  23. background: #1aad19;
  24. }
  25. .label-2__icon {
  26. position: relative;
  27. display: inline-block;
  28. vertical-align: middle;
  29. margin-right: 10px;
  30. width: 18px;
  31. height: 18px;
  32. background: #fcfff4;
  33. border-radius: 50px;
  34. }
  35. .label-2__icon-checked {
  36. position: absolute;
  37. left: 3px;
  38. top: 3px;
  39. width: 12px;
  40. height: 12px;
  41. background: #1aad19;
  42. border-radius: 50%;
  43. }
  44. .label-4_text{
  45. text-align: center;
  46. margin-top: 15px;
  47. }

 label  - 图1

原文:

https://developers.weixin.qq.com/miniprogram/dev/component/label.html