微信小程序常用表單組件的使用詳解
1、常用表單組件
1.1 button
<button>為按鈕組件,是常用的表單組件之一,用于事件的觸發(fā)以及表單的提交。其屬性表如下所示。

代碼示例:
<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)按鈕狀態(tài)</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>為復(fù)選框組件,常用于在表單中進行多項數(shù)據(jù)的選擇。復(fù)選框的<checkbox-group>為父控件,其內(nèi)部嵌套若干個<checkbox>子控件。
<checkbox-group>屬性如下:

<checkbox>組件的屬性如下:

代碼示例:
checkbox.wxml
<view class="demo-box">
<view class="title">8.checkbox小案例</view>
<view class="title">利用for循環(huán)批量生成</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發(fā)生change事件,攜帶value值為:", e.detail.value)
}
})

1.3 input
<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="該輸入框已經(jīng)被禁用"/> <view class="title">(4)為輸入框增加事件監(jiān)聽</view> <input bindinput="getInput" bindblur="getBlur" placeholder="這里輸入的內(nèi)容將會被監(jiān)聽"/> </view>

1.4 label
<label>是標簽組件,不會呈現(xiàn)任何效果,但是可以用來改進表單組件的可用性。當(dāng)用戶在label元素內(nèi)點擊文本時,就會觸發(fā)此控件,即當(dāng)用戶選擇該標簽時,事件會傳遞到和標簽相關(guān)的表單控件上,可以使用for屬性綁定id,也可以將空間放在該標簽內(nèi)部,該組件對應(yīng)屬性如下所示。

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>為表單控件組件,用于提交表單組件中的內(nèi)容。<form>控件組件內(nèi)部可以嵌套多種組件。
組件屬性如下:

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發(fā)生了submit事件,攜帶數(shù)據(jù)為:")
console.log(e.detail.value)
},
onReset() {
console.log("form發(fā)生了reset事件,表單已被重置")
}
})
輸入測試數(shù)據(jù)后點擊注冊按鈕觸發(fā)表單提交事件。

1.6 radio
<radio>為單選框組件,往往需配合<radio-group>組件來使用,<radio>標簽嵌套在<radio-group>當(dāng)中。
<radio-group>組件屬性如下:

<radio>組件屬性如下:

radio.wxml
<view class="demo-box">
<view class="title">14.radio小案例</view>
<view class="title">利用for循環(huán)批量生成</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發(fā)生change事件,攜帶value值為:", e.detail.value)
}
})

1.7 slider
<slider>為滑動選擇器,用于可視化地動態(tài)改變某變量地取值。屬性表如下:

slider.wxml
<view class="demo-box"> <view class="title">15.slider小案例</view> <view class="title">(1)滑動條右側(cè)顯示當(dāng)前進度值</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)增加滑動條監(jiān)聽事件</view> <slider min="0" max="100" value="30" bindchange="sliderChange" /> </view>

1.8 switch
<switch>為開關(guān)選擇器,常用于表單上地開關(guān)功能,屬性表如下所示。

switch.wxml
<view class="demo-box"> <view class="title">16.switch小案例</view> <view class="title">增加switch事件監(jiān)聽</view> <switch checked bindchange="switch1Change"></switch> <switch bindchange="switch2Change"></switch> </view>

1.9 textarea
<textarea>為多行輸入框,常用于多行文字的輸入。
2、實訓(xùn)小案例–問卷調(diào)查
survey.wxml
<view class="content">
<form bindsubmit="onSubmit" bindreset="onReset">
<view class="title">1.你現(xiàn)在大幾?</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="購物"/>網(wǎng)購</label>
<label>
<checkbox value="學(xué)習(xí)"/>學(xué)習(xí)</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.談?wù)勀銓ξ⑿判〕绦蛭磥戆l(fā)展的看法</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("你選擇的現(xiàn)在大幾:", 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("你輸入的對小程序發(fā)展前途的看法是:"+e.detail.value.textarea)
},
onReset() {
console.log("表單已被重置")
}
})

更多內(nèi)容請參考微信官方文檔
以上就是微信小程序常用表單組件的使用詳解的詳細內(nèi)容,更多關(guān)于小程序表單組件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Laydate時間組件在火狐瀏覽器下有多時間輸入框時只能給第一個輸入框賦值的解決方法
這篇文章主要介紹了Laydate時間組件在火狐瀏覽器下有多時間輸入框時只能給第一個輸入框賦值的解決方法,需要的朋友可以參考下2016-08-08

