完整微信小程序(Java后端) 技术贴目录清单页面(必看)
输入框。该组件是原生组件,使用时请注意相关限制
type 的合法值
confirm-type 的合法值
Bug & Tip
tip
: confirm-type的最终表现与手机输入法本身的实现有关,部分安卓系统输入法和第三方输入法可能不支持或不完全支持tip
: input 组件是一个原生组件,字体是系统字体,所以无法设置 font-familytip
: 在 input 聚焦期间,避免使用 css 动画tip
: 对于将input
封装在自定义组件中、而form
在自定义组件外的情况,form
将不能获得这个自定义组件中input
的值。此时需要使用自定义组件的 内置 behaviorswx://form-field
tip
: 键盘高度发生变化,keyboardheightchange事件可能会多次触发,开发者对于相同的height值应该忽略掉bug
: 微信版本6.3.30
, focus 属性设置无效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="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="digit" 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" 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>
index.js
Page({data: {focus: false,inputValue: ''},bindKeyInput: function (e) {this.setData({inputValue: e.detail.value})},bindReplaceInput: function (e) {var value = e.detail.valuevar pos = e.detail.cursorvar leftif (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()}}})
运行效果:
微信搜一搜【java1234】关注这个放荡不羁的程序员,关注后回复【资料】有我准备的一线大厂笔试面试资料以及简历模板。