笔记内容:Form表单类组件与Map地图组件
笔记日期:2018-02-04


form之switch组件

switch组件是一个开关选择器,wxml示例代码如下:

<view class='container'> <view class='switch_text'>switch</view> <switch name='switch2' checked='true' /> <switch name='switch3' /> <switch name='switch4' checked='true' color='red' /></view>

说明:

name属性设置该switch组件的名称checked属性设置该switch组件是否为选中状态,true为选中,false为不选中,不设置该属性的话默认为falsecolor属性设置该switch组件的颜色

样式代码如下:

.container{ text-align: center;}.switch_text{ color: #d1d1d1; margin-bottom: 10rpx;}.container switch{ margin-bottom: 20rpx;}

运行效果:

switch组件里有一个bindchange事件,通过该事件我们可以获得该组件的状态值,wxml代码如下:

<switch name='switch5' bindchange='onBindChange' />

js代码如下:

onBindChange:function(event){ console.log(event.detail.value);}

打印结果:

truefalse

switch组件的官方说明文档如下:

https://mp.weixin.qq.com/debug/wxadoc/dev/component/switch.html


form之slider组件

slider组件是一个滑动选择器,也就是滑动条,wxml示例代码如下:

<view> <view class='title'>slider</view> <slider min='0' max='500' step='100' name='slider' value='100' show-value='true'></slider></view>

说明:

min属性设置该slider组件的最小值max属性设置该slider组件的最大值step属性 设置该slider组件的步长,也就是每拖动一次就递增多少个数值。以上设置的是100,那么每拖动一次就会递增100value属性设置该slider组件默认选中的值show-value属性显示该slider组件当前被选中的值

样式代码如下:

.title{ color: #d1d1d1; margin-bottom: 10rpx; margin-left: 20rpx;}

运行效果:

slider组件的官方说明文档如下:

https://mp.weixin.qq.com/debug/wxadoc/dev/component/slider.html


form之radio-group组件

radio-group是单项选择器,也就是单选框,而其内部由多个&lt;radio/&gt;单选项目组成,示例代码如下:

<view> <view class='title'>radio</view> <view class='radio_view'> <radio-group name='radio-group' bindchange='onRadioChange'> <label> <radio value='漓江塔' checked='true'>漓江塔</radio> </label> <label> <radio value='努巴尼'>努巴尼</radio> </label> <label> <radio value='尼泊尔' disabled='true'>尼泊尔</radio> </label> </radio-group> </view></view>

说明:

disabled属性设置该radio组件为禁用状态

样式代码如下:

.title { color: #d1d1d1; margin-bottom: 10rpx; margin-left: 20rpx;}.radio_view { margin-left: 20rpx; color: #666;}.radio_view label { margin-left: 20rpx; margin-right: 20rpx;}

js代码如下:

onRadioChange: function (event) { console.log(event.detail.value); }

运行效果:

radio组件的官方说明文档如下:

https://mp.weixin.qq.com/debug/wxadoc/dev/component/radio.html


form之checkbox-group组件

checkbox-group是多项选择器,也就是多选框,其内部由多个checkbox组成,示例代码如下:

<view> <view class='title'>checkbox</view> <view class='checkbox_view'> <checkbox-group name='checkbox-group' bindchange='onCheckboxChange'> <label> <checkbox value='漓江塔' checked='true'>漓江塔</checkbox> </label> <label> <checkbox value='努巴尼'>努巴尼</checkbox> </label> <label> <checkbox value='尼泊尔' disabled='true'>尼泊尔</checkbox> </label> </checkbox-group> </view></view>

样式代码如下:

.title { color: #d1d1d1; margin-bottom: 10rpx; margin-left: 20rpx;}.checkbox_view { margin-left: 20rpx; color: #666;}.checkbox_view label { margin-left: 20rpx; margin-right: 20rpx;}

js代码如下:

onCheckboxChange: function (event) { console.log(event.detail.value); }

运行效果:

然后选择多个:

控制台打印出来的是一个数组:

checkbox组件的官方说明文档如下:

https://mp.weixin.qq.com/debug/wxadoc/dev/component/checkbox.html


form表单提交

熟悉web前端开发的小伙伴应该对表单提交都不陌生,表单提交就是把表单组件中的数据都收集起来,然后向服务器进行提交。小程序中也有form组件,它是通过触发事件来进行数据的提交的,小程序的表单提交比web中的表单提交更为简单一些,wxml代码示例:

<view class='page'> <view class='page_hd'> <text class='page_title'>form</text> <text class='page_desc'>表单</text> </view> <form bindsubmit="formSubmit" bindreset="formReset"> <view class="section section_gap"> <view class="section__title">switch</view> <switch name="switch" /> </view> <view class="section section_gap"> <view class="section__title">slider</view> <slider name="slider" show-value min='0' max='100' value='50'></slider> </view> <view class="section"> <view class="section__title">input</view> </view> <input name="input" placeholder="please input here" /> <view class="section section_gap"> <view class="section__title">radio</view> <radio-group name="radio-group"> <label> <radio value="漓江塔" />漓江塔</label> <label> <radio value="努巴尼" />努巴尼</label> <label> <radio value="尼泊尔" />尼泊尔</label> </radio-group> </view> <view class="section section_gap"> <view class="section__title">checkbox</view> <checkbox-group name="checkbox"> <label> <checkbox value="西湖醋鱼" />西湖醋鱼</label> <label> <checkbox value="糖醋排骨" />糖醋排骨</label> <label> <checkbox value="松鼠桂鱼" />松鼠桂鱼</label> <label> <checkbox value="酒酿丸子" />酒酿丸子</label> <label> <checkbox value="鱼香肉丝" />鱼香肉丝</label> </checkbox-group> </view> <view class="btn-area"> <button formType="submit">Submit</button> <button formType="reset">Reset</button> </view> </form></view>

样式代码示例:

.page { display: flex; flex-direction: column; background-color: #fbfbfb;}.page_hd { display: flex; flex-direction: column; align-items: center; margin-bottom: 50rpx; margin-top: 50rpx;}.page_title { font-size: 25rpx; color: #d1d1d1;}.page_desc { text-align: center; font-size: 30rpx; width: 200rpx; color: #d1d1d1; border-bottom: 1px solid #d1d1d1; padding-bottom: 20rpx;}.section__title { margin-bottom: 20rpx; font-size: 32rpx;}.section { font-size: 30rpx; color: #666; padding-left: 30rpx; padding-right: 30rpx;}.page input { width: 100%; height: 80rpx; font-size: 25rpx; background-color: white; padding-left: 30rpx;}.section_gap { margin-top: 60rpx; margin-bottom: 60rpx;}label { display: flex; flex-direction: row; margin-bottom: 10rpx;}.btn-area button { width: 620rpx; margin-bottom: 30rpx;}

js代码示例:

Page({ formSubmit: function (event) { console.log('form发生了submit事件,携带数据为:', event.detail.value); }, formReset: function (event) { console.log('form发生了reset事件'); },})

运行效果:

填写一些数据,然后点击Submit按钮进行表单的提交:

控制台打印的数据如下:

form表单组件的官方说明文档如下:

https://mp.weixin.qq.com/debug/wxadoc/dev/component/form.html


map组件

map组件是用来实现地图功能的,wxml示例代码:

<map id="map" longitude="113.324520" latitude="23.099994" scale="14" markers="{{markers}}" bindmarkertap="markertap" polyline="{{polyline}}" ></map>

说明:

longitude属性用于设置中心经度latitude属性则用于设置中心维度scale属性用于设置地图的缩放级别,取值范围为5-18,也就是我们在手机上能够双指放大缩小的级别markers属性用于设置地图的mark点信息,也就是定位时的那个表示位置或者用于标记位置的图标bindmarkertap表示点击mark点时会触发的事件polyline属性用于设置地图的mark点路线信息

js代码如下:

Page({ // 初始化一些数据 data: { // mark点信息 markers: [{ iconPath: "/images/mark.png", // mark点的图标路径 id: 0, latitude: 23.099994, longitude: 113.324520, width: 50, height: 50 }], // mark点路线信息 polyline: [{ points: [{ longitude: 113.3245211, latitude: 23.10229 }, { longitude: 113.324520, latitude: 23.21229 }], color: "#FF0000DD", width: 3, dottedLine: true }], }, markertap: function (event) { // 调用微信的内置地图 wx.openLocation({ latitude: 23.10229, longitude: 113.3245211, }) },})

运行效果:

点击地图的mark图标触发事件后会进入微信的内置地图:

注:map组件的一些功能在模拟器上不能完全显示出来,所以研究该组件的时候,最好使用真机来调试。

map组件的官方说明文档如下:

https://mp.weixin.qq.com/debug/wxadoc/dev/component/map.html