## form表单
[TOC]
表单,将组件内的用户输入的 `<switch/>` `<input/>` `<checkbox/>` `<slider/>` `<radio/>` `<picker/>` 提交。
当点击 `<form/>` 表单中 formType 为 submit 的 `<button/>` 组件时,会将表单组件中的 value 值进行提交,需要在表单组件中加上 name 来作为 key。
### 事例一
![](https://box.kancloud.cn/b6b18b7ec2e89837d9453e74455e768a_328x314.gif)
#### wxml
```
<form confirm-type="done" bindsubmit="onConfirm">
<view class="comment-head">
<text bind:tap="toggle">取消</text>
<button plain="{{true}}" formType="submit">确定</button>
</view>
<view class="comments-3">
<view class="comment">
<block wx:for="{{tools.filterArr(comments,3)}}" wx:key="{{index}}">
<comment content="{{item.content}}">
<text slot="after" class="num">+{{item.nums}}</text>
</comment>
</block>
</view>
</view>
<view class="input-area">
<input value="{{inputValue}}" name="input" type="text" placeholder="请输入您的短评,可输入12字" bindconfirm="onConfirm"/>
</view>
</form>
```
>1.botton按钮要需要边框需添加`plain="{{true}}"`
绑定表单类型是提交类`formType="submit"`
2.手机键盘下方提交按钮显示完成`confirm-type="done"`
绑定提交事件onConfirm`bindsubmit="onConfirm"`
3.input框添加`name="input"`同样绑定事件`bindconfirm="onConfirm"`
#### wxss
```
.comment-head{
display: flex;
justify-content: space-between;
align-items: center;
padding: 30rpx;
height: 50rpx;
border-bottom:1px solid #ccc;
}
button{
font-size: 30rpx;
font-weight: bold;
background: transparent;
border:none !important;
margin: 0;
padding:0;
}
.input-area{
padding: 30rpx;
}
.input-area input{
padding: 10rpx;
background: #ccc;
border-radius: 20rpx;
}
```
#### js
```
data: {
inputValue:""
},
```
```
onConfirm(e) {
var value = e.detail.value;
//回车提交内容与点击确定提交内容一个是对象一个是字符串,此处进行typeof类型判断,让两值直接相等
if(typeof value != "string"){
value = e.detail.value.input;
}
//判断短评数组存在并且输入value值不为空或空格时进行comments数组的内容添加,unshift添加在原有comments的第一项
if(this.data.comments&&(value!=" ")&&(value!="")){
this.data.comments.unshift({
content:value,
nums:1
})
this.setData({
comments:this.data.comments
,
inputValue:" ",
})
}
}
```
### 事例二
![](https://box.kancloud.cn/3c170e5bee6cd7800202c65b1fd1d974_410x702.gif)
#### wxml
```
<input type="search" confirm-type="search" bindconfirm="onConfirm" class="search" placeholder="请在此输入搜索内容"></input>
<view style="display:{{isShow?'block':'none'}};" class="noMore">没有更多内容了</view>
<view style="display:{{isShow?'none':'block'}};">
<block wx:for="{{movies}}" wx:key="index">
<movieItem data="{{item}}" itemId="{{item.id}}"></movieItem>
</block>
</view>
```
>1.confirm-type的值
![](https://box.kancloud.cn/899d9f01d1d9230076d6aa9d3604d569_765x585.png)
2.bindconfirm点击完成按钮时触发,event.detail = {value: value}
#### wxss
```
.search{
display: block;
box-sizing: border-box;
text-align: center;
border-bottom: 1px solid gainsboro;
font-size:40rpx;
height: 100rpx;
color: gray;
}
.noMore{
text-align: center;
color: rgb(175, 174, 174);
font-size: 30rpx;
margin-top: 70rpx;
}
```
#### js
```
onConfirm(e) {
var value = e.detail.value;
http.request({
url: "/search?q=" + value,
success: res => {
this.handleData(res)
}
})
},
```
>提交input中内容后,获取输入的内容,并依照内容发送http请求,处理数据
- 项目启动配置
- 01.欢迎页
- 03.点击图片切换——事件绑定bindtap、catchtap
- 04.底方分页切换tabBar
- 20.音乐播放
- 06.豆瓣电影列表
- 1.App
- 2.template
- 3.movies-detail 页面详情
- 组件
- 02.地图
- 05.轮播图swiper
- 07.点击预览图片previewImage结合scroll-view
- 11.web-page
- 14.form表单
- 08.头像图片更换chooseImage
- 09.上拉刷新OnReachBottom
- 10.使用缓存(收藏、分享状态)
- 12.导航条加载、页面加载、导航标题设置
- 13.弹出框
- 15.demo
- data
- local.js
- pages
- welcome(欢迎页)
- index
- index.wxml
- index.wxss
- index.js
- index-detail
- index-detail.wxml
- index-detail.wxss
- index-detail.js
- template
- yuedu-list.wxml
- yuedu-list.wxss
- map
- movies
- movies.wxml
- movies.wxss
- movies.js
- movies-grid
- movies-grid-template.wxml
- movies-grid-template.wxss
- movies-item
- movies-item-template.wxml
- movies-item-template.wxss
- movies-more
- movies-more.wxml
- movies-more.wxss
- movies-more.js
- movie-detail
- movie-detail.wxss
- movie-detail.wxml
- movie-detail.js
- star
- star-template.wxml
- star-template.wxss
- web-page
- web-page.wxml
- web-page.js
- post-img
- post-img.wxml
- post-img.js
- post-img.wxss
- 演员列表图
- 列表图.wxml
- 列表图.wxss
- 列表图.js
- utils
- utils.js
- app.js
- app.json
- app.wxss
- 16.事件冒泡
- 18.Tip
- 框架
- 17.模板
- 19.组件模板和样式(slot)
- component构造器
- 组件事件
- behaviors
- wxs
- HTTP封装
- models使用封装HTTP
- 更多有关promise封装的例子
- 生命周期
- 注意: