label.uvue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 checkboxItemsTest label" v-for="item in checkboxItems"
  9. :key="item.name">
  10. <view>
  11. <checkbox :value="item.name" :checked="item.checked"></checkbox>
  12. </view>
  13. <view>{{item.value}}</view>
  14. </label>
  15. </checkbox-group>
  16. </view>
  17. <view class="uni-form-item uni-column">
  18. <view class="title">label用for标识表单组件</view>
  19. <radio-group class="uni-list radio-group" @change="radioChange">
  20. <view class="uni-list-cell uni-list-cell-pd" v-for="(item,index) in radioItems" :key="index">
  21. <view>
  22. <radio :id="item.name" :value="item.name" :checked="item.checked"></radio>
  23. </view>
  24. <label class="label-2-text" :for="item.name">
  25. <text>{{item.value}}</text>
  26. </label>
  27. </view>
  28. </radio-group>
  29. </view>
  30. <view class="uni-form-item uni-column">
  31. <view class="title">label内有多个时选中第一个</view>
  32. <checkbox-group class="uni-list" @change="checkboxForChange">
  33. <label class="label-3 label">
  34. <view class="uni-list-cell uni-list-cell-pd">
  35. <checkbox value="for1">选项一</checkbox>
  36. </view>
  37. <view class="uni-list-cell uni-list-cell-pd">
  38. <checkbox value="for2">选项二</checkbox>
  39. </view>
  40. <view class="uni-center" style="margin:10px 0;">
  41. <text class="uni-link">点击该label下的文字默认选中第一个checkbox</text>
  42. </view>
  43. </label>
  44. </checkbox-group>
  45. </view>
  46. </view>
  47. </view>
  48. </template>
  49. <script lang="uts">
  50. type controlItem = {
  51. name : string,
  52. value : string,
  53. checked ?: Boolean
  54. }
  55. export default {
  56. data() {
  57. return {
  58. title: 'label',
  59. checkboxItems: [{
  60. name: 'USA',
  61. value: '美国'
  62. },
  63. {
  64. name: 'CHN',
  65. value: '中国',
  66. checked: 'true'
  67. }
  68. ] as controlItem[],
  69. radioItems: [{
  70. name: 'USA',
  71. value: '美国'
  72. },
  73. {
  74. name: 'CHN',
  75. value: '中国',
  76. checked: 'true'
  77. }
  78. ] as controlItem[],
  79. hidden: false,
  80. checkboxValue: [] as string[],
  81. checkboxForValue: [] as string[],
  82. radioValue: ''
  83. }
  84. },
  85. methods: {
  86. checkboxChange: function (e : UniCheckboxGroupChangeEvent) {
  87. console.log(e.detail.value)
  88. this.checkboxValue = e.detail.value
  89. },
  90. checkboxForChange: function (e : UniCheckboxGroupChangeEvent) {
  91. console.log(e.detail.value)
  92. this.checkboxForValue = e.detail.value
  93. },
  94. radioChange: function (e : UniRadioGroupChangeEvent) {
  95. console.log(e.detail.value)
  96. this.radioValue = e.detail.value
  97. }
  98. }
  99. }
  100. </script>
  101. <style>
  102. .uni-list-cell {
  103. justify-content: flex-start
  104. }
  105. .uni-list .label-3 {
  106. padding: 0;
  107. }
  108. .label-2-text {
  109. flex: 1;
  110. }
  111. .uni-form-item {
  112. display: flex;
  113. width: 100%;
  114. padding: 5px 0;
  115. }
  116. .uni-form-item .title {
  117. padding: 5px 12px;
  118. }
  119. .radio-group {
  120. padding: 0 10px;
  121. }
  122. .label {
  123. margin: 0 10px;
  124. }
  125. </style>