form

表单,将组件内的用户输入的<switch/> <input/> <checkbox/> <slider/> <radio/> <picker/> 提交。

当点击 <form/> 表单中 formTypesubmit<button/> 组件时,会将表单组件中的 value 值进行提交,需要在表单组件中加上 name 来作为 key

属性名类型默认值说明
bindsubmitEventHandle携带 form 中的数据触发 submit 事件,event.detail = {value : {'name': 'value'} , target: ''}
bindresetEventHandle表单重置时会触发 reset 事件

示例

  1. <view class="container">
  2. <view class="page-body">
  3. <form catchsubmit="formSubmit" catchreset="formReset">
  4. <view class="page-section page-section-gap">
  5. <view class="page-section-title">switch</view>
  6. <switch name="switch"/>
  7. </view>
  8. <view class="page-section page-section-gap">
  9. <view class="page-section-title">radio</view>
  10. <radio-group name="radio">
  11. <label><radio value="radio1"/>选项一</label>
  12. <label><radio value="radio2"/>选项二</label>
  13. </radio-group>
  14. </view>
  15. <view class="page-section page-section-gap">
  16. <view class="page-section-title">checkbox</view>
  17. <checkbox-group name="checkbox">
  18. <label><checkbox value="checkbox1"/>选项一</label>
  19. <label><checkbox value="checkbox2"/>选项二</label>
  20. </checkbox-group>
  21. </view>
  22. <view class="page-section page-section-gap">
  23. <view class="page-section-title">slider</view>
  24. <slider value="50" name="slider" show-value ></slider>
  25. </view>
  26. <view class="page-section">
  27. <view class="page-section-title">input</view>
  28. <view class="page-cells page-cells_after-title">
  29. <view class="page-cell page-cell_input">
  30. <view class="page-cell__bd">
  31. <input class="page-input" name="input" placeholder="这是一个输入框" />
  32. </view>
  33. </view>
  34. </view>
  35. </view>
  36. <view class="btn-area">
  37. <button type="primary" formType="submit">提交</button>
  38. <button formType="reset">重设</button>
  39. </view>
  40. </form>
  41. </view>
  42. </view>
  1. Page({
  2. data: {
  3. pickerHidden: true,
  4. chosen: ''
  5. },
  6. pickerConfirm: function (e) {
  7. this.setData({
  8. pickerHidden: true
  9. })
  10. this.setData({
  11. chosen: e.detail.value
  12. })
  13. },
  14. pickerCancel: function (e) {
  15. this.setData({
  16. pickerHidden: true
  17. })
  18. },
  19. pickerShow: function (e) {
  20. this.setData({
  21. pickerHidden: false
  22. })
  23. },
  24. formSubmit: function (e) {
  25. console.log('form发生了submit事件,携带数据为:', e.detail.value)
  26. },
  27. formReset: function (e) {
  28. console.log('form发生了reset事件,携带数据为:', e.detail.value)
  29. this.setData({
  30. chosen: ''
  31. })
  32. }
  33. })

form - 图1

原文: https://developer.toutiao.com/docs/comp/form.html