form

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

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

属性名 类型 说明 最低版本
report-submit Boolean 是否返回 formId 用于发送模板消息
bindsubmit EventHandle 携带 form 中的数据触发 submit 事件,event.detail = {value : {'name': 'value'} , formId: ''}
bindreset EventHandle 表单重置时会触发 reset 事件

示例代码:

在开发者工具中预览效果

  1. <form bindsubmit="formSubmit" bindreset="formReset">
  2. <view class="section section_gap">
  3. <view class="section__title">switch</view>
  4. <switch name="switch"/>
  5. </view>
  6. <view class="section section_gap">
  7. <view class="section__title">slider</view>
  8. <slider name="slider" show-value ></slider>
  9. </view>
  10. <view class="section">
  11. <view class="section__title">input</view>
  12. <input name="input" placeholder="please input here" />
  13. </view>
  14. <view class="section section_gap">
  15. <view class="section__title">radio</view>
  16. <radio-group name="radio-group">
  17. <label><radio value="radio1"/>radio1</label>
  18. <label><radio value="radio2"/>radio2</label>
  19. </radio-group>
  20. </view>
  21. <view class="section section_gap">
  22. <view class="section__title">checkbox</view>
  23. <checkbox-group name="checkbox">
  24. <label><checkbox value="checkbox1"/>checkbox1</label>
  25. <label><checkbox value="checkbox2"/>checkbox2</label>
  26. </checkbox-group>
  27. </view>
  28. <view class="btn-area">
  29. <button formType="submit">Submit</button>
  30. <button formType="reset">Reset</button>
  31. </view>
  32. </form>
  1. Page({
  2. formSubmit: function(e) {
  3. console.log('form发生了submit事件,携带数据为:', e.detail.value)
  4. },
  5. formReset: function() {
  6. console.log('form发生了reset事件')
  7. }
  8. })

form

原文:

https://developers.weixin.qq.com/miniprogram/dev/component/form.html