本篇文章给大家带来了关于微信小程序的相关知识,其中主要介绍了一些常用表单组件,包括了button、checkbox、input、label等等相关问题,下面一起来看一下,希望对大家有帮助。 ...
本篇文章给大家带来了关于微信小程序的相关知识,其中主要介绍了一些常用表单组件,包括了button、checkbox、input、label等等相关问题,下面一起来看一下,希望对大家有帮助。 【相关学习推荐:小程序学习教程】 1、常用表单组件1.1 button 代码示例: <view class="demo-box"> <view class="title">7.button小案例</view> <view class="title">(1)迷你按钮</view> <button size="mini" type="primary">主要按钮</button> <button size="mini" type="default">次要按钮</button> <button size="mini" type="warn">警告按钮</button> <view class="title">(2)按钮状态</view> <button>普通按钮</button> <button disabled>警用按钮</button> <button loading>加载按钮</button> <view class="title">(3)增加按钮事件</view> <button bindgetuserinfo="getUserDetail" open-type="getUserInfo">点我获取用户信息</button></view> 登录后复制 1.2 checkbox 代码示例: checkbox.wxml <view class="demo-box"> <view class="title">8.checkbox小案例</view> <view class="title">利用for循环批量生成</view> <checkbox-group bindchange="checkboxChange"> <label wx:for="{{items}}"> <checkbox value="{{item.name}}" checked="{{item.checked}}" />{{item.value}} </label> </checkbox-group> </view> 登录后复制 checkbox.js Page({ data: { items: [ { name: "tiger", value: "老虎" }, { name: "elephant", value: "大象" }, { name: "lion", value: "狮子", checked: "true" }, { name: "penguin", value: "企鹅" }, { name: "elk", value: "麋鹿" }, { name: "swan", value: "天鹅" }, ] }, checkboxChange:function(e) { console.log("checkbox发生change事件,携带value值为:", e.detail.value) }}) 登录后复制 1.3 input <view class="demo-box"> <view class="title">9.input小案例</view> <view class="title">(1)文字输入框</view> <input type="text" maxlength="10" placeholder="这里最多只能输入10个字" /> <view class="title">(2)密码输入框</view> <input type="password" placeholder="请输入密码"/> <view class="title">(3)禁用输入框</view> <input disabled placeholder="该输入框已经被禁用"/> <view class="title">(4)为输入框增加事件监听</view> <input bindinput="getInput" bindblur="getBlur" placeholder="这里输入的内容将会被监听"/></view> 登录后复制 1.4 label wxml <view class="demo-box"> <view class="title">10.lable小案例</view> <view class="title">(1)利用for属性</view> <checkbox-group> <checkbox id="tiger" checked /> <label for="tiger">老虎</label> <checkbox id="elephant" /> <label for="elephant">大象</label> <checkbox id="lion" /> <label for="lion">狮子</label> </checkbox-group> <view class="title">(2)label包裹组件</view> <checkbox-group> <label> <checkbox checked />老虎 </label> <label> <checkbox/>大象 </label> <label> <checkbox/>狮子 </label> </checkbox-group></view> 登录后复制 1.5 form 组件属性如下: form.wxml <view class="demo-box"> <view class="title">11.form小案例</view> <view class="title">模拟注册功能</view> <form bindsubmit="onSubmit" bindreset="onReset"> <text>用户名:</text> <input name="username" type="text" placeholder="请输入你的用户名"></input> <text>密码:</text> <input name="password" type="password" placeholder="请输入你的密码"></input> <text>手机号:</text> <input name="phonenumber" type="password" placeholder="请输入你的手机号"></input> <text>验证码:</text> <input name="code" type="password" placeholder="请输入验证码"></input> <button form-type="submit">注册</button> <button form-type="reset">重置</button> </form></view> 登录后复制 form.js Page({ onSubmit(e) { console.log("form发生了submit事件,携带数据为:") console.log(e.detail.value) }, onReset() { console.log("form发生了reset事件,表单已被重置") }}) 登录后复制 输入测试数据后点击注册按钮触发表单提交事件。 1.6 radio radio.wxml <view class="demo-box"> <view class="title">14.radio小案例</view> <view class="title">利用for循环批量生成</view> <radio-group bindchange="radioChange"> <block wx:for="{{radioItems}}"> <radio value="{{item.name}}" checked="{{item.checked}}" />{{item.value}} </block> </radio-group></view> 登录后复制 radio.js Page({ data: { radioItems: [ { name: 'tiger', value: '老虎' }, { name: 'elephant', value: '大象' }, { name: 'lion', value: '狮子', checked: 'true' }, { name: 'penguin', value: '企鹅' }, { name: 'elk', value: '麋鹿' }, { name: 'swan', value: '天鹅' }, ] }, radioChange:function(e) { console.log("radio发生change事件,携带value值为:", e.detail.value) }}) 登录后复制 1.7 slider slider.wxml <view class="demo-box"> <view class="title">15.slider小案例</view> <view class="title">(1)滑动条右侧显示当前进度值</view> <slider min="0" max="100" value="30" show-value /> <view class="title">(2)自定义滑动条颜色与滑块样式</view> <slider min="0" max="100" value="30" block-size="20" block-color="gray" activeColor="skyblue" /> <view class="title">(3)禁用滑动条</view> <slider disabled /> <view class="title">(4)增加滑动条监听事件</view> <slider min="0" max="100" value="30" bindchange="sliderChange" /></view> 登录后复制 1.8 switch switch.wxml <view class="demo-box"> <view class="title">16.switch小案例</view> <view class="title">增加switch事件监听</view> <switch checked bindchange="switch1Change"></switch> <switch bindchange="switch2Change"></switch></view> 登录后复制 1.9 textarea 2、实训小案例–问卷调查survey.wxml <view class="content"> <form bindsubmit="onSubmit" bindreset="onReset"> <view class="title">1.你现在大几?</view> <radio-group bindchange="universityChange"> <radio value="大一"/>大一 <radio value="大二"/>大二 <radio value="大三"/>大三 <radio value="大四"/>大四 </radio-group> <view class="title">2.你使用手机最大的用途是什么?</view> <checkbox-group bindchange="mobilChange"> <label><checkbox value="社交"/>社交</label> <label> <checkbox value="购物"/>网购</label> <label> <checkbox value="学习"/>学习</label><label> <checkbox value="其他"/>其他</label> </checkbox-group> <view class="title">3.平时每天使用手机多少小时?</view> <slider min="0" max="24" show-value bindchange="timechange" /> <view class="title">4.你之前有使用过微信小程序吗?</view> <radio-group bindchange="programChange"> <radio value="无"/>无 <radio value="有"/>有 </radio-group> <view class="title">5.谈谈你对微信小程序未来发展的看法</view> <textarea auto-height placeholder="请输入你的看法" name="textarea" /> <button size="mini" form-type="submit">提交</button> <button size="mini" form-type="reset">重置</button> </form></view> 登录后复制 survey.js Page({ universityChange: function (e) { console.log("你选择的现在大几:", e.detail.value) }, mobilChange: function (e) { console.log("你选择使用手机的最大用途是:", e.detail.value) }, timechange: function (e) { console.log("你选择的每天使用手机的时间是:", e.detail.value + "小时") }, programChange: function (e) { console.log("你选择的是否使用过微信小程序:", e.detail.value) }, onSubmit(e) { console.log("你输入的对小程序发展前途的看法是:"+e.detail.value.textarea) }, onReset() { console.log("表单已被重置") }}) 登录后复制 【相关学习推荐:小程序学习教程】 以上就是归纳整理微信小程序常用表单组件的详细内容,更多请关注模板之家(www.mb5.com.cn)其它相关文章! |