Skip to content

form ​

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

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

属性说明

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

示例 查看演示

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

vue
<template>
  <view>
    <view>
      <form @submit="formSubmit" @reset="formReset">
        <view class="uni-form-item uni-column">
          <view class="title">switch</view>
          <view>
            <switch name="switch" />
          </view>
        </view>
        <view class="uni-form-item uni-column">
          <view class="title">radio</view>
          <radio-group name="radio">
            <label>
              <radio value="radio1" /><text>选项一</text>
            </label>
            <label>
              <radio value="radio2" /><text>选项二</text>
            </label>
          </radio-group>
        </view>
        <view class="uni-form-item uni-column">
          <view class="title">checkbox</view>
          <checkbox-group name="checkbox">
            <label>
              <checkbox value="checkbox1" /><text>选项一</text>
            </label>
            <label>
              <checkbox value="checkbox2" /><text>选项二</text>
            </label>
          </checkbox-group>
        </view>
        <view class="uni-form-item uni-column">
          <view class="title">slider</view>
          <slider value="50" name="slider" show-value></slider>
        </view>
        <view class="uni-form-item uni-column">
          <view class="title">input</view>
          <input class="uni-input" name="input" placeholder="这是一个输入框" />
        </view>
        <view class="uni-btn-v">
          <button form-type="submit">Submit</button>
          <button type="default" form-type="reset">Reset</button>
        </view>
      </form>
    </view>
  </view>
</template>
<template>
  <view>
    <view>
      <form @submit="formSubmit" @reset="formReset">
        <view class="uni-form-item uni-column">
          <view class="title">switch</view>
          <view>
            <switch name="switch" />
          </view>
        </view>
        <view class="uni-form-item uni-column">
          <view class="title">radio</view>
          <radio-group name="radio">
            <label>
              <radio value="radio1" /><text>选项一</text>
            </label>
            <label>
              <radio value="radio2" /><text>选项二</text>
            </label>
          </radio-group>
        </view>
        <view class="uni-form-item uni-column">
          <view class="title">checkbox</view>
          <checkbox-group name="checkbox">
            <label>
              <checkbox value="checkbox1" /><text>选项一</text>
            </label>
            <label>
              <checkbox value="checkbox2" /><text>选项二</text>
            </label>
          </checkbox-group>
        </view>
        <view class="uni-form-item uni-column">
          <view class="title">slider</view>
          <slider value="50" name="slider" show-value></slider>
        </view>
        <view class="uni-form-item uni-column">
          <view class="title">input</view>
          <input class="uni-input" name="input" placeholder="这是一个输入框" />
        </view>
        <view class="uni-btn-v">
          <button form-type="submit">Submit</button>
          <button type="default" form-type="reset">Reset</button>
        </view>
      </form>
    </view>
  </view>
</template>
js
<script>
  export default {
    methods: {
      formSubmit: (theEv) => {
        console.log('form发生了submit事件,携带数据为:' + JSON.stringify(theEv.detail.value));
        var formdata = theEv.detail.value;
        qin.showModal({
          content: '表单数据内容:' + JSON.stringify(formdata),
          showCancel: false,
        });
      },
      formReset: () => {
        console.log('清空数据');
      },
    },
  };
</script>
<script>
  export default {
    methods: {
      formSubmit: (theEv) => {
        console.log('form发生了submit事件,携带数据为:' + JSON.stringify(theEv.detail.value));
        var formdata = theEv.detail.value;
        qin.showModal({
          content: '表单数据内容:' + JSON.stringify(formdata),
          showCancel: false,
        });
      },
      formReset: () => {
        console.log('清空数据');
      },
    },
  };
</script>
css
<style>
  .uni-form-item .title {
    padding: 20rpx 0;
  }
</style>
<style>
  .uni-form-item .title {
    padding: 20rpx 0;
  }
</style>

使用内置 behaviors

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

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

qin://form-field

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

qin://form-field

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

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

示例如下:

vue
<!-- /pages/index/index.vue -->
<template>  
  <view class="content">
    <form @submit="onSubmit">
      <compInput name="test" v-model="testValue"></compInput>  
      <button form-type="submit">Submit</button>  
    </form>
  </view>
</template>
<!-- /pages/index/index.vue -->
<template>  
  <view class="content">
    <form @submit="onSubmit">
      <compInput name="test" v-model="testValue"></compInput>  
      <button form-type="submit">Submit</button>  
    </form>
  </view>
</template>
js
<script>  
  export default {
    data() {
      return {
        testValue: 'Hello',
      };
    },
    methods: {
      onSubmit(e) {
        console.log(e);
      },
    },
  };
</script>
<script>  
  export default {
    data() {
      return {
        testValue: 'Hello',
      };
    },
    methods: {
      onSubmit(e) {
        console.log(e);
      },
    },
  };
</script>
vue
<!-- /components/compInput/compInput.vue -->
<template>
  <view>
    <input
      name="test"
      style="border: solid 1px #999999;height: 80px;"
      type="text"
      @input="onInput"
      :value="value"
    />
  </view>
</template>
<!-- /components/compInput/compInput.vue -->
<template>
  <view>
    <input
      name="test"
      style="border: solid 1px #999999;height: 80px;"
      type="text"
      @input="onInput"
      :value="value"
    />
  </view>
</template>
js
<script>
  export default {
    name: 'compInput',
    behaviors: ['qin://form-field'],
    data(){
      return{
        value: '',
      };
    },
    methods: {
      onInput(e) {
        this.$emit('update:modelValue',e.detail.value);
      },
    },
  };
</script>
<script>
  export default {
    name: 'compInput',
    behaviors: ['qin://form-field'],
    data(){
      return{
        value: '',
      };
    },
    methods: {
      onInput(e) {
        this.$emit('update:modelValue',e.detail.value);
      },
    },
  };
</script>

增强的qin-forms组件

  • 为方便做表单验证,qin ui提供了<uni-forms>组件,参考