Input
输入框。
属性
支持通用属性
| 属性名 | 类型 | 默认值 | 说明 | 平台支持 |
|---|---|---|---|---|
| value | String | 输入框的初始内容 | all | |
| type | String | "text" | input 的类型 | all |
| password | Boolean | false | 是否是密码类型 | all |
| placeholder | String | 输入框为空时占位符 | all | |
| placeholder-style | String | 指定 placeholder 的样式 | all | |
| placeholder-class | String | "input-placeholder" | 指定 placeholder 的样式类 | u?,w,a |
| disabled | Boolean | false | 是否禁用 | all |
| maxlength | Number | 140 | 最大输入长度,设置为 -1 的时候不限制最大长度 | u?,w,a |
| focus | Boolean | false | 获取焦点 | all |
| confirm-type | String | "done" | 设置键盘右下角按钮的文字 | u?,w,a |
| confirm-hold | Boolean | false | 点击键盘右下角按钮时是否保持键盘不收起 | u?,w,a |
type 有效值:
| 值 | 说明 |
|---|---|
| text | 文本输入键盘 |
| number | 数字输入键盘 |
| idcard | 身份证输入键盘 |
| digit | 带小数点的数字键盘 |
confirm-type 有效值:
| 值 | 说明 |
|---|---|
| send | 右下角按钮为“发送” |
| search | 右下角按钮为“搜索” |
| next | 右下角按钮为“下一个” |
| go | 右下角按钮为“前往” |
| done | 右下角按钮为“完成” |
事件
支持通用事件
| 事件名 | 说明 | 平台支持 |
|---|---|---|
| oninput | 键盘输入事件,event.detail={value:value} | all |
| onfocus | 获取焦点事件:event.detail={value:value} | all |
| onblur | 失去焦点事件:event.detail={value:value} | all |
| onconfirm | 点击键盘完成事件:event.detail={value:value} | all |
示例
<!--input.nml-->
<view class="section">
<input placeholder="这是一个可以自动聚焦的input" auto-focus/>
</view>
<view class="section">
<input placeholder="这个只有在按钮点击的时候才聚焦" focus="" />
<view class="btn-area">
<button onclick="bindButtonTap">使得输入框获取焦点</button>
</view>
</view>
<view class="section">
<input maxlength="10" placeholder="最大输入长度10" />
</view>
<view class="section">
<view class="section__title">你输入的是:</view>
<input oninput="bindKeyInput" placeholder="输入同步到view中"/>
</view>
<view class="section">
<input oninput="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: function() {
this.setData({
focus: true
})
},
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){
//光标在中间
var 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'),
}
})