form.md 3.1 KB
Newer Older
M
mehaotian 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#### form
表单,将组件内的用户输入的``<switch>`` ``<input>`` ``<checkbox>`` ``<slider>`` ``<radio>`` ``<picker>`` 提交。

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

**属性说明**

|属性名|类型|说明|平台差异说明|
|:-|:-|:-|:-|
|report-submit|Boolean|是否返回 formId 用于发送[模板消息](https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/template-message.html)|微信小程序、支付宝小程序|
|@submit|EventHandle|携带 form 中的数据触发 submit 事件,event.detail = {value : {'name': 'value'} , formId: ''},report-submit 为 true 时才会返回 formId||
|@reset|EventHandle|表单重置时会触发 reset 事件|&nbsp;|

W
wangyaqi 已提交
14
**示例** [查看演示](https://uniapp.dcloud.io/h5/pages/component/form/form)
M
mehaotian 已提交
15 16 17 18
 
```html
<template>
	<view>
W
wanganxp 已提交
19
		<view>
M
mehaotian 已提交
20 21 22 23 24 25 26 27 28 29 30
			<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>
W
wanganxp 已提交
31
							<radio value="radio1" /><text>选项一</text>
M
mehaotian 已提交
32 33
						</label>
						<label>
W
wanganxp 已提交
34
							<radio value="radio2" /><text>选项二</text>
M
mehaotian 已提交
35 36 37 38 39 40 41
						</label>
					</radio-group>
				</view>
				<view class="uni-form-item uni-column">
					<view class="title">checkbox</view>
					<checkbox-group name="checkbox">
						<label>
W
wanganxp 已提交
42
							<checkbox value="checkbox1" /><text>选项一</text>
M
mehaotian 已提交
43 44
						</label>
						<label>
W
wanganxp 已提交
45
							<checkbox value="checkbox2" /><text>选项二</text>
M
mehaotian 已提交
46 47 48 49 50 51 52 53 54 55 56 57
						</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">
W
wanganxp 已提交
58 59
					<button form-type="submit">Submit</button>
					<button type="default" form-type="reset">Reset</button>
M
mehaotian 已提交
60 61 62 63 64
				</view>
			</form>
		</view>
	</view>
</template>
W
wanganxp 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
<script>
	export default {
		data() {
			return {
			}
		},
		methods: {
			formSubmit: function(e) {
				console.log('form发生了submit事件,携带数据为:' + JSON.stringify(e.detail.value))
				var formdata = e.detail.value
				uni.showModal({
					content: '表单数据内容:' + JSON.stringify(formdata),
					showCancel: false
				});
			},
			formReset: function(e) {
				console.log('清空数据')
			}
		}
	}
</script>

<style>
	.uni-form-item .title {
		padding: 20rpx 0;
	}
</style>

M
mehaotian 已提交
93 94 95 96 97
```
 
![uniapp](https://img-cdn-qiniu.dcloud.net.cn/uniapp/doc/img/form.png?t=201857)

**tips**
W
wanganxp 已提交
98
- [插件市场](http://ext.dcloud.net.cn/search?q=%E8%A1%A8%E5%8D%95%E6%A0%A1%E9%AA%8C)有表单校验插件