简体中文
组件类型:UniInputElement
输入框
Android | iOS | web |
---|---|---|
3.9 | 4.11 | 4.0 |
名称 | 类型 | 默认值 | 描述 | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
name | string | - | 表单的控件名称,作为键值对的一部分与表单(form组件)一同提交 | ||||||||||||||||||
disabled | boolean | false | 是否禁用 | ||||||||||||||||||
value | string | "" | 输入框的初始内容 | ||||||||||||||||||
type | text | number | digit | tel | "text" | input的类型 | ||||||||||||||||||
| |||||||||||||||||||||
password | boolean | false | 是否是密码类型 | ||||||||||||||||||
placeholder | string | "" | 输入框为空时占位符 | ||||||||||||||||||
placeholder-style | string | "" | 指定 placeholder 的样式 | ||||||||||||||||||
placeholder-class | string(string.ClassString) | "" | 指定 placeholder 的样式类,目前仅支持color,font-size和font-weight | ||||||||||||||||||
maxlength | number | "不限制长度" | 最大输入长度,0和正数为合法值,非法值的时候不限制最大长度 | ||||||||||||||||||
cursor-spacing | number | 0 | 指定光标与键盘的距离,单位 px 。取 input 距离底部的距离和 cursor-spacing 指定的距离的最小值作为光标与键盘的距离 | ||||||||||||||||||
cursor-color | string(string.ColorString) | "" | 指定光标颜色 | ||||||||||||||||||
auto-focus | boolean | false | 自动获取焦点 | ||||||||||||||||||
focus | boolean | false | 获取焦点 | ||||||||||||||||||
confirm-type | send | search | next | go | done | "done" | 设置键盘右下角按钮的文字 | ||||||||||||||||||
| |||||||||||||||||||||
confirm-hold | boolean | false | 点击键盘右下角按钮时是否保持键盘不收起 | ||||||||||||||||||
cursor | number | 0 | 指定focus时的光标位置 | ||||||||||||||||||
selection-start | number | -1 | 光标起始位置,自动聚集时有效,需与selection-end搭配使用 | ||||||||||||||||||
selection-end | number | -1 | 光标结束位置,自动聚集时有效,需与selection-satrt搭配使用 | ||||||||||||||||||
adjust-position | boolean | true | 键盘弹起时,是否自动上推页面 | ||||||||||||||||||
hold-keyboard | boolean | false | focus时,点击页面的时候不收起键盘 | ||||||||||||||||||
inputmode | none | text | decimal | numeric | tel | search | email | url | "text" | 是一个枚举属性,它提供了用户在编辑元素或其内容时可能输入的数据类型的提示。在符合条件的高版本webview里,uni-app的 web 和 app-vue 平台中可使用本属性。 | ||||||||||||||||||
| |||||||||||||||||||||
@input | (event: UniInputEvent) => void | - | 当键盘输入时,触发input事件,event.detail = {value, cursor},处理函数可以直接 return 一个字符串,将替换输入框的内容。 | ||||||||||||||||||
@focus | (event: UniInputFocusEvent) => void | - | 输入框聚焦时触发,event.detail = { value, height },height 为键盘高度 | ||||||||||||||||||
@blur | (event: UniInputBlurEvent) => void | - | 输入框失去焦点时触发,event.detail = {value: value} | ||||||||||||||||||
@keyboardheightchange | (event: UniInputKeyboardHeightChangeEvent) => void | - | 键盘高度发生变化的时候触发此事件,event.detail = {height: height, duration: duration} | ||||||||||||||||||
@confirm | (event: UniInputConfirmEvent) => void | - | 点击完成按钮时触发,event.detail = {value: value} |
Android | iOS | web | |
---|---|---|---|
text | 3.9 | 4.11 | 4.0 |
number | 3.9 | 4.11 | 4.0 |
digit | 3.9 | 4.11 | 4.0 |
tel | 3.9 | 4.11 | x |
Android | iOS | web | |
---|---|---|---|
send | 3.9 | 4.11 | 4.0 |
search | 3.9 | 4.11 | 4.0 |
next | 3.9 | 4.11 | 4.0 |
go | 3.9 | 4.11 | 4.0 |
done | 3.9 | 4.11 | 4.0 |
Android | iOS | web | |
---|---|---|---|
none | x | x | 4.0 |
text | x | x | 4.0 |
decimal | x | x | 4.0 |
numeric | x | x | 4.0 |
tel | x | x | 4.0 |
search | x | x | 4.0 |
x | x | 4.0 | |
url | x | x | 4.0 |
名称 | 类型 | 必填 | 默认值 | 描述 | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
detail | UniInputEventDetail | 是 | - | - | ||||||||||||||||||||
| ||||||||||||||||||||||||
bubbles | boolean | 是 | - | 是否冒泡 | ||||||||||||||||||||
cancelable | boolean | 是 | - | 是否可以取消 | ||||||||||||||||||||
type | string | 是 | - | 事件类型 | ||||||||||||||||||||
target | UniElement | null | 否 | - | UVUE DOM 元素对象,描述了 UVUE DOM 元素所普通具有的属性和方法。 | ||||||||||||||||||||
currentTarget | UniElement | null | 否 | - | UVUE DOM 元素对象,描述了 UVUE DOM 元素所普通具有的属性和方法。 | ||||||||||||||||||||
timeStamp | number | 是 | - | 事件发生时的时间戳 |
名称 | 类型 | 必填 | 默认值 | 描述 |
---|---|---|---|---|
stopPropagation | () => void | 是 | - | 阻止当前事件的进一步传播 |
preventDefault | () => void | 是 | - | 阻止当前事件的默认行为 |
名称 | 类型 | 必填 | 默认值 | 描述 | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
detail | UniInputFocusEventDetail | 是 | - | - | |||||||||||||||
| |||||||||||||||||||
bubbles | boolean | 是 | - | 是否冒泡 | |||||||||||||||
cancelable | boolean | 是 | - | 是否可以取消 | |||||||||||||||
type | string | 是 | - | 事件类型 | |||||||||||||||
target | UniElement | null | 否 | - | UVUE DOM 元素对象,描述了 UVUE DOM 元素所普通具有的属性和方法。 | |||||||||||||||
currentTarget | UniElement | null | 否 | - | UVUE DOM 元素对象,描述了 UVUE DOM 元素所普通具有的属性和方法。 | |||||||||||||||
timeStamp | number | 是 | - | 事件发生时的时间戳 |
Android | iOS | web | |
---|---|---|---|
height | 3.9 | 4.11 | x |
名称 | 类型 | 必填 | 默认值 | 描述 |
---|---|---|---|---|
stopPropagation | () => void | 是 | - | 阻止当前事件的进一步传播 |
preventDefault | () => void | 是 | - | 阻止当前事件的默认行为 |
名称 | 类型 | 必填 | 默认值 | 描述 | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
detail | UniInputBlurEventDetail | 是 | - | - | |||||||||||||||
| |||||||||||||||||||
bubbles | boolean | 是 | - | 是否冒泡 | |||||||||||||||
cancelable | boolean | 是 | - | 是否可以取消 | |||||||||||||||
type | string | 是 | - | 事件类型 | |||||||||||||||
target | UniElement | null | 否 | - | UVUE DOM 元素对象,描述了 UVUE DOM 元素所普通具有的属性和方法。 | |||||||||||||||
currentTarget | UniElement | null | 否 | - | UVUE DOM 元素对象,描述了 UVUE DOM 元素所普通具有的属性和方法。 | |||||||||||||||
timeStamp | number | 是 | - | 事件发生时的时间戳 |
名称 | 类型 | 必填 | 默认值 | 描述 |
---|---|---|---|---|
stopPropagation | () => void | 是 | - | 阻止当前事件的进一步传播 |
preventDefault | () => void | 是 | - | 阻止当前事件的默认行为 |
名称 | 类型 | 必填 | 默认值 | 描述 | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
detail | UniInputKeyboardHeightChangeEventDetail | 是 | - | - | |||||||||||||||
| |||||||||||||||||||
bubbles | boolean | 是 | - | 是否冒泡 | |||||||||||||||
cancelable | boolean | 是 | - | 是否可以取消 | |||||||||||||||
type | string | 是 | - | 事件类型 | |||||||||||||||
target | UniElement | null | 否 | - | UVUE DOM 元素对象,描述了 UVUE DOM 元素所普通具有的属性和方法。 | |||||||||||||||
currentTarget | UniElement | null | 否 | - | UVUE DOM 元素对象,描述了 UVUE DOM 元素所普通具有的属性和方法。 | |||||||||||||||
timeStamp | number | 是 | - | 事件发生时的时间戳 |
名称 | 类型 | 必填 | 默认值 | 描述 |
---|---|---|---|---|
stopPropagation | () => void | 是 | - | 阻止当前事件的进一步传播 |
preventDefault | () => void | 是 | - | 阻止当前事件的默认行为 |
名称 | 类型 | 必填 | 默认值 | 描述 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
detail | UniInputConfirmEventDetail | 是 | - | - | ||||||||||
| ||||||||||||||
bubbles | boolean | 是 | - | 是否冒泡 | ||||||||||
cancelable | boolean | 是 | - | 是否可以取消 | ||||||||||
type | string | 是 | - | 事件类型 | ||||||||||
target | UniElement | null | 否 | - | UVUE DOM 元素对象,描述了 UVUE DOM 元素所普通具有的属性和方法。 | ||||||||||
currentTarget | UniElement | null | 否 | - | UVUE DOM 元素对象,描述了 UVUE DOM 元素所普通具有的属性和方法。 | ||||||||||
timeStamp | number | 是 | - | 事件发生时的时间戳 |
名称 | 类型 | 必填 | 默认值 | 描述 |
---|---|---|---|---|
stopPropagation | () => void | 是 | - | 阻止当前事件的进一步传播 |
preventDefault | () => void | 是 | - | 阻止当前事件的默认行为 |
Android | iOS | web | |
---|---|---|---|
name | 3.9 | 4.11 | 4.0 |
disabled | 3.9 | 4.11 | 4.0 |
value | 3.9 | 4.11 | 4.0 |
type | 3.9 | 4.11 | 4.0 |
password | 3.9 | 4.11 | 4.0 |
placeholder | 3.9 | 4.11 | 4.0 |
placeholder-style | 3.9 | 4.11 | 4.0 |
placeholder-class | 3.9 | 4.11 | 4.0 |
maxlength | 3.9 | 4.11 | 4.0 |
cursor-spacing | 3.9 | 4.11 | x |
cursor-color | 3.99 | 4.11 | 4.0 |
auto-focus | 3.9 | 4.11 | 4.0 |
focus | 3.9 | 4.11 | 4.0 |
confirm-type | 3.9 | 4.11 | 4.0 |
confirm-hold | 3.9 | 4.11 | 4.0 |
cursor | 3.9 | 4.11 | 4.0 |
selection-start | 3.9 | 4.11 | 4.0 |
selection-end | 3.9 | 4.11 | 4.0 |
adjust-position | 3.9 | 4.11 | x |
hold-keyboard | 4.0 | 4.11 | x |
inputmode | x | x | 4.0 |
@input | 3.9 | 4.11 | 4.0 |
@focus | 3.9 | 4.11 | 4.0 |
@blur | 3.9 | 4.11 | 4.0 |
@keyboardheightchange | 3.9 | 4.11 | x |
@confirm | 3.9 | 4.11 | 4.0 |
不可以嵌套组件
Template
Script
<template>
<!-- #ifdef APP -->
<scroll-view style="flex: 1">
<!-- #endif -->
<page-head :title="title"></page-head>
<view class="uni-common-mt uni-padding-wrap">
<view>
<view class="uni-title">
<text class="uni-title-text">设置输入框的初始内容</text>
</view>
<view class="input-wrapper">
<input id="uni-input-default" class="uni-input" value="hello uni-app x" />
</view>
</view>
<view>
<view class="uni-title">
<text class="uni-title-text">type取值(不同输入法表现可能不一致)</text>
</view>
<view class="input-wrapper">
<input id="uni-input-type-text" class="uni-input" type="text" placeholder="文本输入键盘" />
</view>
<view class="input-wrapper">
<input id="uni-input-type-number" class="uni-input" type="number" placeholder="数字输入键盘" />
</view>
<view class="input-wrapper">
<input id="uni-input-type-digit" class="uni-input" type="digit" placeholder="带小数点的数字输入键盘" />
</view>
<view class="input-wrapper">
<input id="uni-input-type-tel" class="uni-input" :type="inputTypeTel" placeholder="电话输入键盘" />
</view>
</view>
<view>
<view class="uni-title">
<text class="uni-title-text">密码输入框</text>
</view>
<view class="input-wrapper">
<input id="uni-input-password" class="uni-input" :password="inputPassword" :value="inputPasswordValue" />
</view>
</view>
<view>
<view class="uni-title">
<text class="uni-title-text">占位符样式</text>
</view>
<view class="input-wrapper">
<input id="uni-input-placeholder1" class="uni-input" :placeholder-style="inputPlaceHolderStyle"
placeholder="占位符文字颜色为红色" />
</view>
<view class="input-wrapper">
<input id="uni-input-placeholder2" class="uni-input" :placeholder-class="inputPlaceHolderClass"
placeholder="占位符字体大小为10px" />
</view>
<view class="input-wrapper">
<input class="uni-input" :placeholder-style="inputPlaceHolderStyle"
value="不设置placeholder只设置placeholder-style" />
</view>
</view>
<view>
<view class="uni-title">
<text class="uni-title-text">设置禁用输入框</text>
</view>
<view class="input-wrapper">
<input id="uni-input-disable" class="uni-input" :disabled="true"/>
</view>
</view>
<view>
<view class="uni-title">
<text class="uni-title-text">设置最大输入长度</text>
</view>
<view class="input-wrapper">
<input id="uni-input-maxlength" class="uni-input" :maxlength="10" placeholder="最大输入长度为10"
:value="inputMaxLengthValue" @input="onMaxLengthInput" :focus="inputMaxLengthFocus" />
</view>
</view>
<view>
<view class="uni-title">
<text class="uni-title-text">设置光标与键盘的距离</text>
</view>
<view class="input-wrapper">
<input class="uni-input" :cursor-spacing="1000" placeholder="光标与键盘的距离为1000px" />
</view>
</view>
<view>
<view class="uni-title">
<text class="uni-title-text">自动获取焦点</text>
</view>
<view class="input-wrapper">
<input id="uni-input-focus" class="uni-input" :focus="focus"
@keyboardheightchange="inputFocusKeyBoardChange" />
</view>
</view>
<view>
<view class="uni-title">
<text class="uni-title-text">confirm-type取值(不同输入法表现可能不一致)</text>
</view>
<view class="input-wrapper">
<input id="uni-input-confirm-send" class="uni-input" confirmType="send" placeholder="键盘右下角按钮显示为发送" />
</view>
<view class="input-wrapper">
<input id="uni-input-confirm-search" class="uni-input" confirmType="search" placeholder="键盘右下角按钮显示为搜索" />
</view>
<view class="input-wrapper">
<input id="uni-input-confirm-next" class="uni-input" confirmType="next" placeholder="键盘右下角按钮显示为下一个" />
</view>
<view class="input-wrapper">
<input id="uni-input-confirm-go" class="uni-input" confirmType="go" placeholder="键盘右下角按钮显示为前往" />
</view>
<view class="input-wrapper">
<input id="uni-input-confirm-done" class="uni-input" confirmType="done" placeholder="键盘右下角按钮显示为完成" />
</view>
</view>
<view>
<view class="uni-title">
<text class="uni-title-text">点击键盘右下角按钮时保持键盘不收起</text>
</view>
<view class="input-wrapper">
<input class="uni-input" :confirm-hold="true" />
</view>
</view>
<view>
<view class="uni-title" @click="setCursor(4)">
<text class="uni-title-text">设置输入框聚焦时光标的位置(点击生效)</text>
</view>
<view class="input-wrapper">
<input ref="input" class="uni-input" value="0123456789" :cursor="cursor" @blur="onCursorBlurChange"/>
</view>
</view>
<view>
<view class="uni-title" @click="setSelection(0, 4)">
<text class="uni-title-text">设置输入框聚焦时光标的起始位置和结束位置(点击生效)</text>
</view>
<view class="input-wrapper">
<input ref="input2" class="uni-input" value="0123456789" :selection-start="selectionStart"
:selection-end="selectionEnd" @blur="onSelectionBlurChange"/>
</view>
</view>
<view>
<view class="uni-title" style="flex-direction: row;align-items: center;">
<text class="uni-title-text">设置光标颜色为红色</text>
<switch style="margin-left: 10px;" @change="changeCursorColor" :checked="cursor_color=='red'"></switch>
</view>
<view class="input-wrapper">
<input id="uni-input-cursor-color" class="uni-input" :cursor-color="cursor_color" value="光标颜色" />
</view>
</view>
<view>
<view class="uni-title">
<text class="uni-title-text">键盘弹起时,自动上推页面</text>
</view>
<view class="input-wrapper">
<input class="uni-input" :adjust-position="true" />
</view>
</view>
<view>
<view class="uni-title" style="flex-direction: row;align-items: center;">
<text class="uni-title-text">设置hold-keyboard</text>
<switch style="margin-left: 10px;" @change="changeHoldKeyboard" :checked="holdKeyboard"></switch>
</view>
<view class="input-wrapper">
<input class="uni-input" :hold-keyboard="holdKeyboard" value="hold keyboard " />
</view>
</view>
<view>
<view class="uni-title">
<text class="uni-title-text">input事件</text>
<text class="uni-subtitle-text" v-if="inputEventDetail">{{inputEventDetail}}</text>
</view>
<view class="input-wrapper">
<input class="uni-input" @input="onInput" />
</view>
</view>
<view>
<view class="uni-title">
<text class="uni-title-text">focus事件和blur事件</text>
<text class="uni-subtitle-text" v-if="focusAndBlurEventDetail">{{focusAndBlurEventDetail}}</text>
</view>
<view class="input-wrapper">
<input class="uni-input" @focus="onFocus" @blur="onBlur" />
</view>
</view>
<view>
<view class="uni-title">
<text class="uni-title-text">confirm事件</text>
<text class="uni-subtitle-text" v-if="confirmEventDetail">{{confirmEventDetail}}</text>
</view>
<view class="input-wrapper">
<input class="uni-input" @confirm="onConfirm" />
</view>
</view>
<view>
<view class="uni-title">
<text class="uni-title-text">keyboardheightchange事件</text>
<text class="uni-subtitle-text"
v-if="keyboardHeightChangeEventDetail">{{keyboardHeightChangeEventDetail}}</text>
</view>
<view class="input-wrapper">
<input class="uni-input" @keyboardheightchange="onKeyborardHeightChange" />
</view>
</view>
<view>
<view class="uni-title">
<text class="uni-title-text">带清除按钮的输入框</text>
</view>
<view class="input-wrapper">
<input class="uni-input" placeholder="带清除按钮的输入框" :value="inputClearValue" @input="clearInput" />
<image class="uni-icon" src="/static/icons/clear.png" v-if="showClearIcon" @click="clearIcon">
</image>
</view>
</view>
<view>
<view class="uni-title">
<text class="uni-title-text">可查看密码的输入框</text>
</view>
<view class="input-wrapper">
<input class="uni-input" placeholder="请输入密码" :password="showPassword" />
<image class="uni-icon" :src="!showPassword ? '/static/icons/eye-active.png' : '/static/icons/eye.png'"
@click="changePassword"></image>
</view>
</view>
</view>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<style scoped>
.input-wrapper {
display: flex;
padding: 8px 13px;
margin: 5px 0;
flex-direction: row;
flex-wrap: nowrap;
background-color: #ffffff;
}
.uni-input {
height: 28px;
font-size: 15px;
padding: 0px;
flex: 1;
background-color: #ffffff;
}
.uni-icon {
width: 24px;
height: 24px;
}
.uni-input-placeholder-class {
font-size: 10px;
}
</style>