【愚公系列】2022年03月 微信小程序

阅读: 评论:0

【愚公系列】2022年03月 微信小程序

【愚公系列】2022年03月 微信小程序

文章目录

  • 前言
    • 1.Form表单的定义
    • 2.Form表单的属性
  • 一、Form表单
    • 1.Form表单基础使用
    • 2.使用内置behaviors
      • 2.1 wx://form-field
      • 2.2 wx://form-field-group
      • 2.3 wx://form-field-button


前言

1.Form表单的定义

表单在网页中主要负责数据采集功能。一个表单有三个基本组成部分:

  • 表单标签:这里面包含了处理表单数据所用CGI程序的URL以及数据提交到服务器的方法。

  • 表单域:包含了文本框、密码框、隐藏域、多行文本框、复选框、单选框、下拉选择框和文件上传框等。

  • 表单按钮:包括提交按钮、复位按钮和一般按钮;用于将数据传送到服务器上的CGI脚本或者取消输入,还可以用表单按钮来控制其他定义了处理脚本的处理工作。

2.Form表单的属性

属性类型默认值必填说明最低版本
report-submitbooleanfalse是否返回 formId 用于发送模板消息1.0.0
report-submit-timeoutnumber0等待一段时间(毫秒数)以确认 formId 是否生效。如果未指定这个参数,formId 有很小的概率是无效的(如遇到网络失败的情况)。指定这个参数将可以检测 formId 是否有效,以这个参数的时间作为这项检测的超时时间。如果失败,将返回 requestFormId:fail 开头的 formId2.6.2
bindsubmiteventhandle携带 form 中的数据触发 submit 事件,event.detail = {value : {‘name’: ‘value’} , formId: ‘’}1.0.0
bindreseteventhandle表单重置时会触发 reset 事件1.0.0

一、Form表单

1.Form表单基础使用

<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 ></slider></view><view class="section"><view class="section__title">input</view><input name="input" placeholder="please input here" /></view><view class="section section_gap"><view class="section__title">radio</view><radio-group name="radio-group"><label><radio value="radio1"/>radio1</label><label><radio value="radio2"/>radio2</label></radio-group></view><view class="section section_gap"><view class="section__title">checkbox</view><checkbox-group name="checkbox"><label><checkbox value="checkbox1"/>checkbox1</label><label><checkbox value="checkbox2"/>checkbox2</label></checkbox-group></view><view class="btn-area"><button formType="submit">Submit</button><button formType="reset">Reset</button></view>
</form>
Page({formSubmit: function (e) {console.log('form发生了submit事件,携带数据为:', e.detail.value)},formReset: function () {console.log('form发生了reset事件')}
})

2.使用内置behaviors

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

  • wx://form-field
  • wx://form-field-group
  • wx://form-field-button

2.1 wx://form-field

属性名类型描述最低版本
nameString在表单中的字段名1.6.7
value任意在表单中的字段值1.6.7

1、组件定义

Component({behaviors: ['wx://form-field'],data: {value: ''},methods: {onChange: function (e) {this.setData({value: e.detail.value,})}}
})
<input placeholder="请输入名字" value="{{value}}" bindinput="onChange" ></input>
input {margin: 60rpx 30rpx;border-bottom: 1px solid grey;padding-bottom: 2px;
}

2、表单使用

{"usingComponents": {"custom-form-field": "../components/custom-form-field/custom-form-field"}
}
<form bindsubmit="formSubmit"><text>表单</text><custom-form-field name="custom-name"></custom-form-field><button form-type="submit">提交</button>
</form>
text {font-size: 40rpx;margin: 30rpx;
}
button {margin: 30rpx;
}
const app = getApp()Page({formSubmit: function(e) {console.info('表单提交携带数据', e.detail.value)},
})

2.2 wx://form-field-group

1、组件定义

// components/custom-comp.js
Component({behaviors: ['wx://form-field-group']
})
<view><text>姓名: </text><input name="name" />
</view>
<view><text>学生:</text><switch name="student" />
</view>
view {display: flex;flex-direction: row;align-items: center;margin: 30rpx;
}
text {margin-right: 30rpx;
}
input {padding-bottom: 1px;border-bottom: 1px solid gray;
}

2、表单使用

{"usingComponents": {"custom-comp": "../components/custom-comp"}
}
<form bindsubmit="formSubmit"><custom-comp></custom-comp><button type="primary" form-type="submit">在控制台查看输出</button>
</form>
.intro {margin: 30px;text-align: center;
}
const app = getApp()Page({data: {},formSubmit: function(e) {console.info('表单提交携带数据', e.detail.value)},onLoad: function () {},
})

2.3 wx://form-field-button

1、组件定义

Component({behaviors: ['wx://form-field-button']
})
 <button type="primary" form-type="submit">在控制台查看输出</button>

2、表单使用

{"usingComponents": {"custom-comp": "../components/custom-comp"}
}
 <form bindsubmit="submit"><input name="name" placeholder="请输入名字"></input><custom-comp></custom-comp></form>
input {margin: 30rpx;padding-bottom: 1px;border-bottom: 1px solid gray;
}
const app = getApp()Page({data: {},submit: function (e) {console.log("表单携带的数据:", e.detail.value)},onLoad: function () {},
})

本文发布于:2024-02-02 12:57:08,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170684982543948.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:愚公   程序   系列   微信小
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23