WeChat applet development documentation
/ 微信小程序表單組件輸入框 input
微信小程序表單組件輸入框 input
微信小程序輸入框input
<!--input.wxml--> <view class="section"> <input placeholder="這是一個(gè)可以自動(dòng)聚焦的input" auto-focus/> </view> <view class="section"> <input placeholder="這個(gè)只有在按鈕點(diǎn)擊的時(shí)候才聚焦" focus="{{focus}}" /> <view class="btn-area"> <button bindtap="bindButtonTap">使得輸入框獲取焦點(diǎn)</button> </view> </view> <view class="section"> <input maxlength="10" placeholder="最大輸入長(zhǎng)度10" /> </view> <view class="section"> <view class="section__title">你輸入的是:{{inputValue}}</view> <input bindinput="bindKeyInput" placeholder="輸入同步到view中"/> </view> <view class="section"> <input bindinput="bindReplaceInput" placeholder="連續(xù)的兩個(gè)1會(huì)變成2" /> </view> <view class="section"> <input bindinput="bindHideKeyboard" placeholder="輸入123自動(dòng)收起鍵盤(pán)" /> </view> <view class="section"> <input type="emoji" placeholder="這是一個(gè)帶有表情的輸入框" /> </view> <view class="section"> <input password type="number" /> </view> <view class="section"> <input password type="text" /> </view> <view class="section"> <input type="digit" placeholder="帶小數(shù)點(diǎn)的數(shù)字鍵盤(pán)"/> </view> <view class="section"> <input type="idcard" placeholder="身份證輸入鍵盤(pán)" /> </view> <view class="section"> <input placeholder-style="color:red" placeholder="占位符字體是紅色的" /> </view>
//input.js Page({ data:{ focus:false, inputValue:"" }, bindButtonTap:function(){ this.setData({ focus:Date.now() }) }, bindKeyInput:function(e){ this.setData({ inputValue:e.detail.value }) }, bindReplaceInput:function(e){ var value = e.detail.value; var pos = e.detail.cursor; if(pos != -1){ //光標(biāo)在中間 var left = e.detail.value.slice(0,pos); //計(jì)算光標(biāo)的位置 pos = left.replace(/11/g,'2').length; } //直接返回對(duì)象,可以對(duì)輸入進(jìn)行過(guò)濾處理,同時(shí)可以控制光標(biāo)的位置 return { value:value.replace(/11/g,'2'), cursor:pos } //或者直接返回字符串,光標(biāo)在最后邊 //return value.replace(/11/g,'2'), }, bindHideKeyboard:function(e){ if(e.detail.value === "123"){ //收起鍵盤(pán) wx.hideKeyboard(); } } })