表单,将组件内的用户输入的<switch> <input> <checkbox> <slider> <radio> <picker> 提交。
当点击 <form> 表单中 formType 为 submit 的 <button> 组件时,会将表单组件中的 value 值进行提交,需要在表单组件中加上 name 来作为 key。

属性说明

属性名 类型 说明 平台差异说明
report-submit Boolean 是否返回 formId 用于发送模板消息 微信小程序、支付宝小程序
report-submit-timeout number 等待一段时间(毫秒数)以确认 formId 是否生效。如果未指定这个参数,formId 有很小的概率是无效的(如遇到网络失败的情况)。指定这个参数将可以检测 formId 是否有效,以这个参数的时间作为这项检测的超时时间。如果失败,将返回 requestFormId:fail 开头的 formId 微信小程序2.6.2
@submit EventHandle 携带 form 中的数据触发 submit 事件,event.detail = {value : {‘name’: ‘value’} , formId: ‘’},report-submit 为 true 时才会返回 formId
@reset EventHandle 表单重置时会触发 reset 事件

示例

查看演示
以下示例代码,来自于hello uni-app项目,推荐使用HBuilderX,新建uni-app项目,选择hello uni-app模板,可直接体验完整示例。

  1. <!-- 本示例未包含完整css,获取外链css请参考上文,在hello uni-app项目中查看 -->
  2. <template>
  3. <view>
  4. <view>
  5. <form @submit="formSubmit" @reset="formReset">
  6. <view class="uni-form-item uni-column">
  7. <view class="title">switch</view>
  8. <view>
  9. <switch name="switch" />
  10. </view>
  11. </view>
  12. <view class="uni-form-item uni-column">
  13. <view class="title">radio</view>
  14. <radio-group name="radio">
  15. <label>
  16. <radio value="radio1" /><text>选项一</text>
  17. </label>
  18. <label>
  19. <radio value="radio2" /><text>选项二</text>
  20. </label>
  21. </radio-group>
  22. </view>
  23. <view class="uni-form-item uni-column">
  24. <view class="title">checkbox</view>
  25. <checkbox-group name="checkbox">
  26. <label>
  27. <checkbox value="checkbox1" /><text>选项一</text>
  28. </label>
  29. <label>
  30. <checkbox value="checkbox2" /><text>选项二</text>
  31. </label>
  32. </checkbox-group>
  33. </view>
  34. <view class="uni-form-item uni-column">
  35. <view class="title">slider</view>
  36. <slider value="50" name="slider" show-value></slider>
  37. </view>
  38. <view class="uni-form-item uni-column">
  39. <view class="title">input</view>
  40. <input class="uni-input" name="input" placeholder="这是一个输入框" />
  41. </view>
  42. <view class="uni-btn-v">
  43. <button form-type="submit">Submit</button>
  44. <button type="default" form-type="reset">Reset</button>
  45. </view>
  46. </form>
  47. </view>
  48. </view>
  49. </template>
  50. <script>
  51. export default {
  52. data() {
  53. return {
  54. }
  55. },
  56. methods: {
  57. formSubmit: function(e) {
  58. console.log('form发生了submit事件,携带数据为:' + JSON.stringify(e.detail.value))
  59. var formdata = e.detail.value
  60. uni.showModal({
  61. content: '表单数据内容:' + JSON.stringify(formdata),
  62. showCancel: false
  63. });
  64. },
  65. formReset: function(e) {
  66. console.log('清空数据')
  67. }
  68. }
  69. }
  70. </script>
  71. <style>
  72. .uni-form-item .title {
  73. padding: 20rpx 0;
  74. }
  75. </style>

form - 图1

Tips

  • 为方便做表单验证,uni ui提供了**<uni-forms>**组件,参考:https://ext.dcloud.net.cn/plugin?id=2773
  • 如果使用uniCloud,其数据库提供了DB Schema,在schema中配置字段的格式,前端表单校验和服务器入参校验将可以复用该规则,无需在两端重复开发。详见
  • 有很多表单自助生成辅助工具
    • 如果使用uniCloud的DB Schema可以自动生成全套表单,包括界面、校验逻辑、提交入库,详见.
    • 不使用uniCloud的话,插件市场有可视化拖拽表单插件:详见。这类插件只生成界面,没有逻辑。

      使用内置 behaviors

      小程序端在form内的自定义组件内有input表单控件时,
      或者用普通标签实现表单控件,
      例如评分等,
      无法在formsubmit事件内获取组件内表单控件值,
      此时可以使用behaviors

对于 form 组件,目前可以自动识别下列内置 behaviors:
uni://form-field

目前仅支持 微信小程序、QQ小程序、百度小程序、h5。

uni://form-field

使自定义组件有类似于表单控件的行为。
form 组件可以识别这些自定义组件,
并在 submit 事件中返回组件的字段名及其对应字段值。
这将为它添加以下两个属性。

属性名 类型 描述
name String 在表单中的字段名
value 任意 在表单中的字段值

示例如下:

  1. <!-- /pages/index/index.vue -->
  2. <template>
  3. <view class="content">
  4. <form @submit="onSubmit">
  5. <comp-input name="test" v-model="testValue"></comp-input>
  6. <button form-type="submit">Submit</button>
  7. </form>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. data() {
  13. return {
  14. testValue: 'Hello'
  15. }
  16. },
  17. methods: {
  18. onSubmit(e) {
  19. console.log(e)
  20. }
  21. }
  22. }
  23. </script>
  24. <style>
  25. </style>
  1. <!-- /components/compInput/compInput.vue -->
  2. <template>
  3. <view>
  4. <input name="test" style="border: solid 1px #999999;height: 80px;" type="text" @input="onInput" :value="value" />
  5. </view>
  6. </template>
  7. <script>
  8. export default {
  9. name: 'compInput',
  10. behaviors: ['uni://form-field'],
  11. methods: {
  12. onInput(e) {
  13. this.$emit('input', e.detail.value)
  14. }
  15. }
  16. }
  17. </script>
  18. <style>
  19. </style>