switch

开关选择器。

属性名类型默认值说明
heckedBooleanfalse是否选中
typeStringswitch样式,有效值:switch, checkbox
bindchangeEventHandlechecked 改变时触发 change 事件,event.detail={ value:checked}
colorColorswitch 的颜色,同 css 的 color

示例代码

  1. <!--switch.jxml-->
  2. <view class="page-section page-section-gap">
  3. <view class="page-section-title">显示效果为 switch</view>
  4. <view class="body-view">
  5. <switch color="{{color}}" style="margin-right: 100rpx" bindchange="switch1Change" />
  6. <switch color="{{color}}" checked bindchange="switch2Change" />
  7. </view>
  8. </view>
  9. <view class="page-section page-section-gap">
  10. <view class="page-section-title">显示效果为 checkbox</view>
  11. <view class="body-view">
  12. <switch color="{{color}}" style="margin-right: 170rpx" type="checkbox" bindchange="switch1Change" />
  13. <switch color="{{color}}" type="checkbox" checked bindchange="switch2Change" />
  14. </view>
  15. </view>
  16. <view >设置当前 switch 颜色</view>
  17. <view class="page-control">
  18. <view class="page-control-row">
  19. <radio-group class="radio-group control-radio" bindchange="changeColor">
  20. <label class="radio" jd:for="{{colorList}}" jd:key="{{item.name}}">
  21. <radio value="{{item.name}}" checked="{{item.checked}}" />{{item.value}}
  22. </label>
  23. </radio-group>
  24. </view>
  25. </view>
  1. //switch.js
  2. Page({
  3. data: {
  4. color: '',
  5. colorList: [{
  6. name: 'blue',
  7. value: "蓝色"
  8. }, {
  9. name: '#333',
  10. value: "黑色"
  11. }, {
  12. name: '',
  13. value: "默认样式",
  14. checked: 'true'
  15. }],
  16. },
  17. switch1Change: function (e) {
  18. console.log('switch1 发生 change 事件,携带值为', e.detail.value);
  19. },
  20. switch2Change: function (e) {
  21. console.log('switch2 发生 change 事件,携带值为', e.detail.value);
  22. },
  23. changeColor(e) {
  24. console.log(e);
  25. this.setData({
  26. color: e.detail.value
  27. });
  28. }
  29. });

switch - 图1