微信小程序表單組件輸入框 input

2022-05-11 16:07 更新

input

基礎庫 1.0.0 開始支持,低版本需做兼容處理。

輸入框。該組件是原生組件,使用時請注意相關限制

屬性 類型 默認值 必填 說明 最低版本
value string 輸入框的初始內容 1.0.0
type string text input 的類型 1.0.0
password boolean false 是否是密碼類型 1.0.0
placeholder string 輸入框為空時占位符 1.0.0
placeholder-style string 指定 placeholder 的樣式 1.0.0
placeholder-class string input-placeholder 指定 placeholder 的樣式類 1.0.0
disabled boolean false 是否禁用 1.0.0
maxlength number 140 最大輸入長度,設置為 -1 的時候不限制最大長度 1.0.0
cursor-spacing number 0 指定光標與鍵盤的距離,取 input 距離底部的距離和 cursor-spacing 指定的距離的最小值作為光標與鍵盤的距離 1.0.0
auto-focus boolean false (即將廢棄,請直接使用 focus )自動聚焦,拉起鍵盤 1.0.0
focus boolean false 獲取焦點 1.0.0
confirm-type string done 設置鍵盤右下角按鈕的文字,僅在type='text'時生效 1.1.0
confirm-hold boolean false 點擊鍵盤右下角按鈕時是否保持鍵盤不收起 1.1.0
cursor number 指定focus時的光標位置 1.5.0
selection-start number -1 光標起始位置,自動聚集時有效,需與selection-end搭配使用 1.9.0
selection-end number -1 光標結束位置,自動聚集時有效,需與selection-start搭配使用 1.9.0
adjust-position boolean true 鍵盤彈起時,是否自動上推頁面 1.9.90
hold-keyboard boolean false focus時,點擊頁面的時候不收起鍵盤 2.8.2
bindinput eventhandle 鍵盤輸入時觸發(fā),event.detail = {value, cursor, keyCode},keyCode 為鍵值,2.1.0 起支持,處理函數(shù)可以直接 return 一個字符串,將替換輸入框的內容。 1.0.0
bindfocus eventhandle 輸入框聚焦時觸發(fā),event.detail = { value, height },height 為鍵盤高度,在基礎庫 1.9.90 起支持 1.0.0
bindblur eventhandle 輸入框失去焦點時觸發(fā),event.detail = {value: value} 1.0.0
bindconfirm eventhandle 點擊完成按鈕時觸發(fā),event.detail = {value: value} 1.0.0
bindkeyboardheightchange eventhandle 鍵盤高度發(fā)生變化的時候觸發(fā)此事件,event.detail = {height: height, duration: duration} 2.7.0

type 的合法值

說明 最低版本
text 文本輸入鍵盤
number 數(shù)字輸入鍵盤
idcard 身份證輸入鍵盤
digit 帶小數(shù)點的數(shù)字鍵盤

confirm-type 的合法值

說明 最低版本
send 右下角按鈕為“發(fā)送”
search 右下角按鈕為“搜索”
next 右下角按鈕為“下一個”
go 右下角按鈕為“前往”
done 右下角按鈕為“完成”

提示:

  1. confirm-type的最終表現(xiàn)與手機輸入法本身的實現(xiàn)有關,部分安卓系統(tǒng)輸入法和第三方輸入法可能不支持或不完全支持
  2. input 組件是一個原生組件,字體是系統(tǒng)字體,所以無法設置 font-family
  3. 在 input 聚焦期間,避免使用 css 動畫
  4. 對于將 input 封裝在自定義組件中、而 form 在自定義組件外的情況, form 將不能獲得這個自定義組件中 input 的值。此時需要使用自定義組件的 內置 behaviors wx://form-field
  5. 鍵盤高度發(fā)生變化,keyboardheightchange事件可能會多次觸發(fā),開發(fā)者對于相同的height值應該忽略掉
  6. 微信版本 6.3.30, focus 屬性設置無效
  7. 微信版本 6.3.30, placeholder 在聚焦時出現(xiàn)重影問題

示例代碼:

<view class="page-body">
  <view class="page-section">
    <view class="weui-cells__title">可以自動聚焦的input</view>
    <view class="weui-cells weui-cells_after-title">
      <view class="weui-cell weui-cell_input">
        <input class="weui-input" auto-focus placeholder="將會獲取焦點"/>
      </view>
    </view>
  </view>
  <view class="page-section">
    <view class="weui-cells__title">控制最大輸入長度的input</view>
    <view class="weui-cells weui-cells_after-title">
      <view class="weui-cell weui-cell_input">
        <input class="weui-input" maxlength="10" placeholder="最大輸入長度為10" />
      </view>
    </view>
  </view>
  <view class="page-section">
    <view class="weui-cells__title">實時獲取輸入值:{{inputValue}}</view>
    <view class="weui-cells weui-cells_after-title">
      <view class="weui-cell weui-cell_input">
        <input class="weui-input"  maxlength="10" bindinput="bindKeyInput" placeholder="輸入同步到view中"/>
      </view>
    </view>
  </view>
  <view class="page-section">
    <view class="weui-cells__title">控制輸入的input</view>
    <view class="weui-cells weui-cells_after-title">
      <view class="weui-cell weui-cell_input">
        <input class="weui-input"  bindinput="bindReplaceInput" placeholder="連續(xù)的兩個1會變成2" />
      </view>
    </view>
  </view>
  <view class="page-section">
    <view class="weui-cells__title">控制鍵盤的input</view>
    <view class="weui-cells weui-cells_after-title">
      <view class="weui-cell weui-cell_input">
        <input class="weui-input"  bindinput="bindHideKeyboard" placeholder="輸入123自動收起鍵盤" />
      </view>
    </view>
  </view>
  <view class="page-section">
    <view class="weui-cells__title">數(shù)字輸入的input</view>
    <view class="weui-cells weui-cells_after-title">
      <view class="weui-cell weui-cell_input">
        <input class="weui-input" type="number" placeholder="這是一個數(shù)字輸入框" />
      </view>
    </view>
  </view>
  <view class="page-section">
    <view class="weui-cells__title">密碼輸入的input</view>
    <view class="weui-cells weui-cells_after-title">
      <view class="weui-cell weui-cell_input">
        <input class="weui-input" password type="text" placeholder="這是一個密碼輸入框" />
      </view>
    </view>
  </view>
  <view class="page-section">
    <view class="weui-cells__title">帶小數(shù)點的input</view>
    <view class="weui-cells weui-cells_after-title">
      <view class="weui-cell weui-cell_input">
        <input class="weui-input" type="digit" placeholder="帶小數(shù)點的數(shù)字鍵盤"/>
      </view>
    </view>
  </view>
  <view class="page-section">
    <view class="weui-cells__title">身份證輸入的input</view>
    <view class="weui-cells weui-cells_after-title">
      <view class="weui-cell weui-cell_input">
        <input class="weui-input" type="idcard" placeholder="身份證輸入鍵盤" />
      </view>
    </view>
  </view>
  <view class="page-section">
    <view class="weui-cells__title">控制占位符顏色的input</view>
    <view class="weui-cells weui-cells_after-title">
      <view class="weui-cell weui-cell_input">
        <input class="weui-input" placeholder-style="color:#F76260" placeholder="占位符字體是紅色的" />
      </view>
    </view>
  </view>
</view>
Page({
  data: {
    focus: false,
    inputValue: ''
  },
  bindKeyInput: function (e) {
    this.setData({
      inputValue: e.detail.value
    })
  },
  bindReplaceInput: function (e) {
    var value = e.detail.value
    var pos = e.detail.cursor
    var left
    if (pos !== -1) {
      // 光標在中間
      left = e.detail.value.slice(0, pos)
      // 計算光標的位置
      pos = left.replace(/11/g, '2').length
    }

    // 直接返回對象,可以對輸入進行過濾處理,同時可以控制光標的位置
    return {
      value: value.replace(/11/g, '2'),
      cursor: pos
    }

    // 或者直接返回字符串,光標在最后邊
    // return value.replace(/11/g,'2'),
  },
  bindHideKeyboard: function (e) {
    if (e.detail.value === '123') {
      // 收起鍵盤
      wx.hideKeyboard()
    }
  }
})

1595573794(1)


以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號