后浪云百度小程序教程:label 表单组件标签

  • label 表单组件标签
    • 属性说明
    • 示例
      • 代码示例 1:label 用 for 标识表单组件
      • 代码示例 2:表单组件在 label 内
      • 代码示例 3:label 内有多个选项时,选中第一个
      • 代码示例 4:label 可控制热区

    label 表单组件标签

    解释:表单组件标签,用来改进表单的可用性。使用 for 属性找到对应的 ID(必须写 for),当点击时,就会触发对应的控件。for 优先级高于内部控件,内部有多个控件的时候默认触发第一个控件。目前可以绑定的控件有:<button><checkbox><radio><switch>

    属性说明

    属性名 类型 默认值 必填 说明

    for

    String

    绑定控件的 ID

    示例

    跳转编辑工具

    在开发者工具中打开

    在 WEB IDE 中打开

    扫码体验

    代码示例

    后浪云百度小程序教程:label 表单组件标签插图

    请使用百度APP扫码

    代码示例 1:label 用 for 标识表单组件

    • SWAN
    • JS
     
     
     
    1. <view class="wrap">
    2. <view class="card-area">
    3. <view class="top-description border-bottom">label用for标识表单组件</view>
    4. <radio-group class="group">
    5. <view s-for="item, index in radioItems">
    6. <label class="block {{item.checked == 'true' ? 'border-bottom': ''}}">
    7. <radio id="{{item.name}}" value="{{item.name}}" checked="{{item.checked}}"></radio>
    8. <label for="{{item.name}}"><text>{{item.value}}</text></label>
    9. </label>
    10. </view>
    11. </radio-group>
    12. </view>
    13. </view>

    代码示例 2:表单组件在 label 内

    • SWAN
    • JS
     
     
     
    1. <view class="wrap">
    2. <view class="card-area">
    3. <view class="top-description border-bottom">表单组件在label内</view>
    4. <checkbox-group class="group">
    5. <view s-for="item, index in checkboxItems">
    6. <label class="block {{item.checked == 'true' ? 'border-bottom': ''}}">
    7. <checkbox value="{{item.name}}" checked="{{item.checked}}"></checkbox>
    8. <text>{{item.value}}</text>
    9. </label>
    10. </view>
    11. </checkbox-group>
    12. </view>
    13. </view>

    代码示例 3:label 内有多个选项时,选中第一个

    • SWAN
     
     
     
    1. <view class="wrap">
    2. <view class="card-area">
    3. <view class="top-description border-bottom">label内有多个选项时,选中第一个</view>
    4. <label>
    5. <label class="block border-bottom">
    6. <checkbox>选项一</checkbox>
    7. </label>
    8. <label class="block border-bottom">
    9. <checkbox>选项二</checkbox>
    10. </label>
    11. <label class="block border-bottom">
    12. <checkbox>选项三</checkbox>
    13. </label>
    14. <view class="near-button">
    15. 点击选中第一项
    16. </view>
    17. </label>
    18. </view>
    19. </view>

    代码示例 4:label 可控制热区

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • SWAN
    • CSS
     
     
     
    1. <view class="wrap">
    2. <view class="card-area">
    3. <view class="top-description">控制热区为整行</view>
    4. <label class="label border-bottom">
    5. <checkbox/>
    6. <text>智能小程序</text>
    7. </label>
    8. <label class="label border-bottom">
    9. <radio/>
    10. <text>智能小程序</text>
    11. </label>
    12. <label class="label border-bottom">
    13. <switch/>
    14. <text style="vertical-align:.1rem">智能小程序</text>
    15. </label>
    16. </view>
    17. </view>
    THE END