label.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-common-mt">
  5. <view class="uni-form-item uni-column">
  6. <view class="title">表单组件在label内</view>
  7. <checkbox-group class="uni-list" @change="checkboxChange">
  8. <label class="uni-list-cell uni-list-cell-pd" v-for="item in checkboxItems" :key="item.name">
  9. <view>
  10. <checkbox :value="item.name" :checked="item.checked"></checkbox>
  11. </view>
  12. <view>{{item.value}}</view>
  13. </label>
  14. </checkbox-group>
  15. </view>
  16. <view class="uni-form-item uni-column">
  17. <view class="title">label用for标识表单组件</view>
  18. <radio-group class="uni-list" @change="radioChange">
  19. <view class="uni-list-cell uni-list-cell-pd" v-for="(item,index) in radioItems" :key="index">
  20. <view>
  21. <radio :id="item.name" :value="item.name" :checked="item.checked"></radio>
  22. </view>
  23. <label class="label-2-text" :for="item.name">
  24. <text>{{item.value}}</text>
  25. </label>
  26. </view>
  27. </radio-group>
  28. </view>
  29. <view class="uni-form-item uni-column">
  30. <view class="title">label内有多个时选中第一个</view>
  31. <checkbox-group class="uni-list" @change="checkboxChange">
  32. <label class="label-3">
  33. <view class="uni-list-cell uni-list-cell-pd">
  34. <checkbox class="checkbox-3">选项一</checkbox>
  35. </view>
  36. <view class="uni-list-cell uni-list-cell-pd">
  37. <checkbox class="checkbox-3">选项二</checkbox>
  38. </view>
  39. <view class="uni-link uni-center" style="margin-top:20rpx;">点击该label下的文字默认选中第一个checkbox</view>
  40. </label>
  41. </checkbox-group>
  42. </view>
  43. </view>
  44. </view>
  45. </template>
  46. <script>
  47. export default {
  48. data() {
  49. return {
  50. title: 'label',
  51. checkboxItems: [{
  52. name: 'USA',
  53. value: '美国'
  54. },
  55. {
  56. name: 'CHN',
  57. value: '中国',
  58. checked: 'true'
  59. }
  60. ],
  61. radioItems: [{
  62. name: 'USA',
  63. value: '美国'
  64. },
  65. {
  66. name: 'CHN',
  67. value: '中国',
  68. checked: 'true'
  69. }
  70. ],
  71. hidden: false
  72. }
  73. },
  74. methods: {
  75. checkboxChange: function(e) {
  76. var checked = e.detail.value
  77. console.log(checked)
  78. },
  79. radioChange: function(e) {
  80. var checked = e.detail.value
  81. console.log(checked)
  82. }
  83. }
  84. }
  85. </script>
  86. <style>
  87. .uni-list-cell {
  88. justify-content: flex-start
  89. }
  90. .uni-list .label-3 {
  91. padding: 0;
  92. }
  93. .label-2-text {
  94. flex: 1;
  95. }
  96. </style>