当前位置 博文首页 > java1234的博客:微信小程序 输入框 input 组件

    java1234的博客:微信小程序 输入框 input 组件

    作者:[db:作者] 时间:2021-06-06 14:21

    完整微信小程序(Java后端) 技术贴目录清单页面(必看)

    输入框。该组件是原生组件,使用时请注意相关限制

    属性类型默认值必填说明最低版本
    valuestring输入框的初始内容1.0.0
    typestringtextinput 的类型1.0.0
    passwordbooleanfalse是否是密码类型1.0.0
    placeholderstring输入框为空时占位符1.0.0
    placeholder-stylestring指定 placeholder 的样式1.0.0
    placeholder-classstringinput-placeholder指定 placeholder 的样式类1.0.0
    disabledbooleanfalse是否禁用1.0.0
    maxlengthnumber140最大输入长度,设置为 -1 的时候不限制最大长度1.0.0
    cursor-spacingnumber0指定光标与键盘的距离,取 input 距离底部的距离和 cursor-spacing 指定的距离的最小值作为光标与键盘的距离1.0.0
    auto-focusbooleanfalse(即将废弃,请直接使用 focus )自动聚焦,拉起键盘1.0.0
    focusbooleanfalse获取焦点1.0.0
    confirm-typestringdone设置键盘右下角按钮的文字,仅在type='text’时生效1.1.0
    always-embedbooleanfalse强制 input 处于同层状态,默认 focus 时 input 会切到非同层状态 (仅在 iOS 下生效)2.10.4
    confirm-holdbooleanfalse点击键盘右下角按钮时是否保持键盘不收起1.1.0
    cursornumber指定focus时的光标位置1.5.0
    selection-startnumber-1光标起始位置,自动聚集时有效,需与selection-end搭配使用1.9.0
    selection-endnumber-1光标结束位置,自动聚集时有效,需与selection-start搭配使用1.9.0
    adjust-positionbooleantrue键盘弹起时,是否自动上推页面1.9.90
    hold-keyboardbooleanfalsefocus时,点击页面的时候不收起键盘2.8.2
    bindinputeventhandle键盘输入时触发,event.detail = {value, cursor, keyCode},keyCode 为键值,2.1.0 起支持,处理函数可以直接 return 一个字符串,将替换输入框的内容。1.0.0
    bindfocuseventhandle输入框聚焦时触发,event.detail = { value, height },height 为键盘高度,在基础库 1.9.90 起支持1.0.0
    bindblureventhandle输入框失去焦点时触发,event.detail = {value: value}1.0.0
    bindconfirmeventhandle点击完成按钮时触发,event.detail = {value: value}1.0.0
    bindkeyboardheightchangeeventhandle键盘高度发生变化的时候触发此事件,event.detail = {height: height, duration: duration}2.7.0

    type 的合法值

    说明最低版本
    text文本输入键盘
    number数字输入键盘
    idcard身份证输入键盘
    digit带小数点的数字键盘

    confirm-type 的合法值

    说明最低版本
    send右下角按钮为“发送”
    search右下角按钮为“搜索”
    next右下角按钮为“下一个”
    go右下角按钮为“前往”
    done右下角按钮为“完成”

    Bug & Tip

    1. tip: confirm-type的最终表现与手机输入法本身的实现有关,部分安卓系统输入法和第三方输入法可能不支持或不完全支持
    2. tip : input 组件是一个原生组件,字体是系统字体,所以无法设置 font-family
    3. tip : 在 input 聚焦期间,避免使用 css 动画
    4. tip : 对于将 input 封装在自定义组件中、而 form 在自定义组件外的情况, form 将不能获得这个自定义组件中 input 的值。此时需要使用自定义组件的 内置 behaviors wx://form-field
    5. tip : 键盘高度发生变化,keyboardheightchange事件可能会多次触发,开发者对于相同的height值应该忽略掉
    6. bug : 微信版本 6.3.30, focus 属性设置无效
    7. bug : 微信版本 6.3.30, placeholder 在聚焦时出现重影问题

    示例代码

    在开发者工具中预览效果

    index.wxml

    <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="连续的两个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">数字输入的input</view>
        <view class="weui-cells weui-cells_after-title">
          <view class="weui-cell weui-cell_input">
            <input class="weui-input" type="number" 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" password type="text" placeholder="这是一个密码输入框" />
          </view>
        </view>
      </view>
      <view class="