目录

input

概述

提供可交互的界面,接收用户的输入,默认为单行。

使用限制

限制条件

说明

适用终端

手机、平板、智慧屏、车机

适用区域

全球

子组件

不支持

属性

除了支持 通用属性 以外,还支持如下属性。

名称

类型

默认值

是否必填

描述

type

button | checkbox | radio | text | email | date | time | number | password

text

否

支持动态修改。

checked

boolean

false

否

当前组件的checked状态,可触发checked伪类,type为checkbox时生效。

name

string

-

否

input组件名称。

value

string

-

否

input组件的值。

placeholder

string

-

否

提示文本的内容,type为button|checkbox|radio|text|email|date|time|number时生效。

maxlength

number

-

否

输入框可输入的最多字符数量。不填表示不限制输入框中的字符数量。

enterkeytype

default | next | go | done | send | search

default

否

不支持动态修改。

设置软键盘Enter按钮的显示文本

  • default:默认显示
  • next:下一个
  • go:开始
  • done:完成
  • send:发送
  • search:搜索
说明:

不同输入法设置相同的值,显示文本可能存在差异。

model:value(1100+)

string

-

否

用于绑定和更新框架中的值的model指令。

样式

<input> 支持active伪类,除了支持 通用样式 以外,还支持如下样式。

名称

类型

默认值

是否必填

描述

color

color

rgba(0,0,0,0.87)

否

文本颜色。

font-size

number

37.5px

否

文本尺寸。

placeholder-color

color

rgba(0,0,0,0.38)

否

提示文本的颜色,type为button|checkbox|radio|text|email|date|time|number|password时生效。

font-weight(1030+)

lighter | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900 | normal | bold | bolder

normal

否

字体的粗细,默认为normal。

width

length | percentage

-

否

type为button时,默认值为128px。

height

length | percentage

-

否

type为button时,默认值为70px。

font-family(1030+)

string

-

否

通过该属性可以设置组件中字符串的字体。

支持四种系统原生字体:normal,sans-serif,serif,monospace(此四种字体只对英文有效)。

如果需要设置自定义字体,请参见“font-face样式”。

caret-color(1060+)

color | transparent | auto

auto

否

自定义光标颜色。

事件

<input> 支持 change 事件,当input组件的值、checked状态发生变化时触发。除了支持通用事件以外,还支持如下事件。

参数

text | email | date | time | number | password

checkbox

radio

备注

name

-

√

√

-

value

√

√

√

当type类型为 checkbox | radio | text | email | date | time | number | password 时,获取input更新后的值需要通过change事件中的e.value。使用this.$element('xx').value将无法获取到值。

详细例子请参见案例“获取input框输入值异常”。

checked

-

√

-

-

enterkeyclick

{value:valuestring}

-

-

软键盘enter键点击事件。

selectionchange(1030+)

√

-

-

在<input>组件中调用其select()和setSelectionRange()方法改变选中字符串时触发此事件。

注意:

<input> 组件类型为text|email|date|time|number时,才触发此事件。

方法

名称

参数

描述

focus

{focus:true|false},focus不传默认为true

使组件获得或者失去焦点,可触发focus伪类,type为text|email|date|time|number|password时,可弹出或收起输入法。

select

-

选中文本框的全部文本。

setSelectionRange

{start: number, end : number}

设置文本框的选中区域。

getSelectionRange

{callback : function(start : number, end : number)}

获取文本框的选中区域。

示例代码

<template>
    <div class="container">
        <!-- Various types of input examples -->
        <div class="input-item">
            <text class="input-hint">input type="text"</text>
            <input class="input-text" id="selectionInput" placeholder="please enter text" maxlength="100" style="placeholder-color:#FF0000;caret-color:#7b68ee;"
                value="{{inputValue}}" onchange="updateValue" onselectionchange="selectionchange"></input>
            <div class="input-item1">
                <input class="input-button color-3" type="button"  value="set focus true"
                    onclick="truefocus">
               </input>
                <input class="input-button color-3" type="button"  value="set focus false"
                    onclick="falsefocus">
               </input>
                <input class="input-button color-3" type="button" value="Set the value" onclick="setValue">
               </input>
                <input class="input-button color-3" type="button" value="clear the value" onclick="resetValue">
               </input>
                <input class="input-button color-3" type="button" value="select all" onclick="selectAll">
               </input>
                <input class="input-button color-3" type="button" value="select [0-2]" onclick="setSelectionRange">
               </input>
                <input class="input-button color-3" type="button" value="get select []" onclick="getSelectionRange">
               </input>
            </div>
        </div>
        <div class="input-item">
            <text class="input-hint">input type="password"</text>
            <input class="input-text" type="password" placeholder="please enter password" onchange="showChangePrompt"></input>
        </div>
        <!-- radio -->
        <div class="input-item">
            <text class="input-hint">input type="radio":</text>
            <div class="doc-row">
                <label target="radio1">label1:</label>
                <input id="radio1" class="flex-grow" type="radio" name="radio" value="radio1" onchange="showChangePrompt">
               </input>
                <label target="radio2">label2:</label>
                <input id="radio2" class="flex-grow" type="radio" name="radio" value="radio2" checked="true" onchange="showChangePrompt">
               </input>
                <label target="radio3">label3:</label>
                <input id="radio3" class="flex-grow" type="radio" name="radio" value="radio3" onchange="showChangePrompt">
               </input>
            </div>
        </div>
        <!-- checkbox -->
        <div class="input-item">
            <text class="input-hint">input type="checkbox":</text>
            <div class="doc-row">
                <label target="checkbox1">label1:</label>
                <input id="checkbox1" class="flex-grow" type="checkbox" name="checkbox" value="checkbox1" onchange="showChangePrompt">
               </input>
                <label target="checkbox2">label2:</label>
                <input id="checkbox2" class="flex-grow" type="checkbox" name="checkbox" value="checkbox2" checked="true"
                    onchange="showChangePrompt">
               </input>
                <label target="checkbox3">label3:</label>
                <input id="checkbox3" class="flex-grow" type="checkbox" name="checkbox" value="checkbox3" onchange="showChangePrompt">
               </input>
            </div>
        </div>
        <!-- Set the enterkeytype and enterkeyclick examples -->
        <div class="input-item">
            <text class="input-hint">enterkeytype = next search [go] done send default</text>
            <input class="input-text" enterkeytype="go" onenterkeyclick="enterkeyclicked" placeholder="enterkeytype = go"
                onchange="showChangePrompt" style="background-color:#fafafa;">
            </input>
        </div>
        <div class="input-item">
            <text class="input-hint">enterkeytype = next search go [done] send default</text>
            <input class="input-text" enterkeytype="done" onenterkeyclick="enterkeyclicked" placeholder="enterkeytype = done"
                onchange="showChangePrompt" style="background-color:#fafafa;">
            </input>
        </div>
        <div class="input-item">
            <text class="input-hint">enterkeytype = next search go done [send] default</text>
            <input class="input-text" enterkeytype="send" onenterkeyclick="enterkeyclicked" placeholder="enterkeytype = send"
                onchange="showChangePrompt" style="background-color:#fafafa;">
            </input>
        </div>
        <div class="input-item">
            <text class="input-hint">enterkeytype = next search go done send [default]</text>
            <input class="input-text" onenterkeyclick="enterkeyclicked" placeholder="enterkeytype = default" onchange="showChangePrompt"
                style="background-color:#fafafa;">
            </input>
        </div>
        <!-- font-family -->
        <div class="input-item">
            <input class="input-text input-fontfamily" value="this input font-family is serif" style="background-color:#fafafa;margin-bottom:30px;"></input>
        </div>
    </div>
</template>
<style>
    .container {
        flex: 1;
        flex-direction: column;
    }
    .input-item {
        margin-bottom: 80px;
        margin-top: 10px;
        flex-direction: column;
    }
    .input-item1 {
        margin: 20px;
        flex-direction: row;
        justify-content:space-between;
        align-items: center;
        flex-wrap: wrap;
    }
    .input-hint {
        margin-left: 30px;
        margin-bottom: 30px;
        margin-right: 30px;
    }
    .doc-row {
        justify-content: center;
        margin-left: 30px;
        margin-right: 30px;
    }
    .flex-grow {
        flex-grow: 1;
    }
    .input-text {
        height: 80px;
        line-height: 80px;
        padding-left: 30px;
        padding-right: 30px;
        margin-left: 30px;
        margin-right: 30px;
        border-top-width: 1px;
        border-bottom-width: 1px;
        border-color: #999999;
        font-size: 30px;
        background-color: #ffffff;
    }
    .input-text:focus {
        border-color: #f76160;
    }
    .input-button {
        font-size: 30px;
         margin-top: 10px;
        border-radius: 5px;
        color: #ffffff;
         background-color: #00bfff;
    }
    .input-fontfamily {
        font-family: serif;
    }
</style>
<script>
    import prompt from '@system.prompt'
    export default {
        data: {
            inputValue: ''
        },
        onInit() {
            this.$page.setTitleBar({ text: 'input1' });
        },
        updateValue(e) {
            this.inputValue = e.value;
        },
        setValue() {
            this.inputValue = "hello world";
        },
        resetValue() {
            this.inputValue = ''
        },
        showChangePrompt(e) {
            prompt.showToast({
                message: (e.name === undefined ? '' : '---name: ' + e.name)
                    + (e.value === undefined ? '' : '---value: ' + e.value)
                    + (e.checked === undefined ? '' : '---checked: ' + e.checked)
                    + (e.text === undefined ? '' : '---text: ' + e.text)
            })
        },
        enterkeyclicked(e) {
            let msg = "enterkeyclicked-->" + "value:" + e.value;
            console.log(msg);
            prompt.showToast({ message: msg });
        },
        truefocus(e) {
            this.$element('selectionInput').focus({ focus: true })
            prompt.showToast({
                message: "get focus"
            })
        },
        falsefocus(e) {
            this.$element('selectionInput').focus({ focus: false })
            prompt.showToast({
                message: "lose focus"
            })
        },
        showClickPrompt(msg) {
            prompt.showToast({
                message: msg
            })
        },
        selectAll() {
            this.$element('selectionInput').select();
        },
        setSelectionRange() {
            this.$element('selectionInput').setSelectionRange({ start: 0, end: 3 });
            //start=3 end=3 Can move the cursor to index=3
            //this.$element('selectionInput').setSelectionRange({start : 3, end : 3});  
        },
        getSelectionRange() {
            this.$element('selectionInput').getSelectionRange({
                callback: function (start, end) {
                    let msg = "getSelectionRange start:" + start + " end:" + end;
                    prompt.showToast({
                        message: msg
                    })
                }
            })
        },
        selectionchange() {
            console.log("selectionchange");
        }
    }
</script>

示例代码效果图:

版本更新说明

版本

发布日期

描述

1100

2022-07-07

新增属性model:value,支持双向绑定 model指令。

1060

2019-12-10

增加样式caret-color用于自定义光标颜色。

1030

2018-10-31

  • type属性支持动态修改。
  • 新增font-family样式的设置,支持四种系统原生字体:normal , sans-serif , serif , monospace。
  • font-weight样式新增支持类型:500、600、700、800、900、bolder为粗体,其他为正常字体。
  • 新增selectionchange事件,在input组件中选中字符串的范围发生变化时触发。

1010

2018-04-20

  • 支持active伪类效果。
  • 增加了maxlength和enterkeytype属性。
  • change事件增加enterkeyclick参数。
  • 新增select、setSelectionRange、getSelectionRange方法。

相关链接

[h2]FAQ

[h2]案例