2-4 input组件

https://developers.weixin.qq.com/miniprogram/dev/component/input.html

input

基础库 1.0.0 开始支持,低版本需做兼容处理

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

属性类型默认值必填说明最低版本
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
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
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

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. bug : 微信版本 6.3.30, focus 属性设置无效
  6. bug : 微信版本 6.3.30, placeholder 在聚焦时出现重影问题

示例代码

在开发者工具中预览效果

<!--input.wxml-->
<view class="section">
  <input placeholder="这是一个可以自动聚焦的input" auto-focus />
</view>
<view class="section">
  <input placeholder="这个只有在按钮点击的时候才聚焦" focus="{{focus}}" />
  <view class="btn-area">
    <button bindtap="bindButtonTap">使得输入框获取焦点</button>
  </view>
</view>
<view class="section">
  <input maxlength="10" placeholder="最大输入长度10" />
</view>
<view class="section">
  <view class="section__title">你输入的是:{{inputValue}}</view>
  <input bindinput="bindKeyInput" placeholder="输入同步到view中" />
</view>
<view class="section">
  <input bindinput="bindReplaceInput" placeholder="连续的两个1会变成2" />
</view>
<view class="section"><input password type="number" /></view>
<view class="section"><input password type="text" /></view>
<view class="section">
  <input type="digit" placeholder="带小数点的数字键盘" />
</view>
<view class="section">
  <input type="idcard" placeholder="身份证输入键盘" />
</view>
<view class="section">
  <input placeholder-style="color:red" placeholder="占位符字体是红色的" />
</view>
// input.js
Page({
  data: {
    focus: false,
    inputValue: ''
  },
  bindButtonTap() {
    this.setData({
      focus: true
    })
  },
  bindKeyInput(e) {
    this.setData({
      inputValue: e.detail.value
    })
  },
  bindReplaceInput(e) {
    const value = e.detail.value
    let pos = e.detail.cursor
    if (pos != -1) {
      // 光标在中间
      const 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'),
  }
})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值