百度智能小程序 輸入框

2020-09-01 10:51 更新

input 輸入框

解釋?zhuān)狠斎肟颍╲3.105.0 起支持同層渲染)。

屬性說(shuō)明

屬性名 類(lèi)型 默認(rèn)值 必填 說(shuō)明 Web 態(tài)說(shuō)明

value

String

輸入框的初始內(nèi)容。若要?jiǎng)討B(tài)設(shè)置輸入框內(nèi)容,需設(shè)置 value="{= value =}"(注:若要取鍵盤(pán)輸入后的 value 請(qǐng)通過(guò) bindinput 獲取)

-

type

String

text

input 的類(lèi)型

-

password

Boolean

false

是否是密碼類(lèi)型

-

placeholder

String

輸入框?yàn)榭諘r(shí)占位符

-

placeholder-style

String

placeholder 的樣式

-

placeholder-class

String

input-placeholder

placeholder 的樣式類(lèi)

-

disabled

Boolean

false

是否禁用

-

maxlength

Number

140

最大輸入長(zhǎng)度,設(shè)置為 -1 的時(shí)候不限制最大長(zhǎng)度

-

cursor-spacing

Number

0

指定光標(biāo)與鍵盤(pán)的距離,單位 px。 當(dāng)鍵盤(pán)彈出時(shí), 如果需要頁(yè)面上滑才能完整顯示 input 組件, 那么此時(shí)光標(biāo)與鍵盤(pán)的距離為設(shè)定的 cursor-spacing 值; 如果 input 組件處于屏幕上方,鍵盤(pán)彈出時(shí)不會(huì)擋住 input, 則忽略該屬性。

受限于設(shè)備系統(tǒng),暫不支持

focus

Boolean

false

獲取焦點(diǎn),調(diào)起鍵盤(pán)。
開(kāi)發(fā)者工具暫不支持自動(dòng)獲取焦點(diǎn)

部分瀏覽器下不支持自動(dòng)獲取焦點(diǎn)

confirm-type

String

done

設(shè)置鍵盤(pán)右下角按鈕的文字

暫不支持

confirm-hold

Boolean

false

點(diǎn)擊鍵盤(pán)右下角按鈕時(shí)是否保持鍵盤(pán)不收起

-

cursor

Number

指定 focus 時(shí)的光標(biāo)位置
開(kāi)發(fā)者工具暫不支持

-

selection-start

Number

-1

光標(biāo)起始位置,自動(dòng)聚焦時(shí)有效,需與 selection-end 搭配使用

-

selection-end

Number

-1

光標(biāo)結(jié)束位置,自動(dòng)聚焦時(shí)有效,需與 selection-start 搭配使用

-

adjust-position

Boolean

true

鍵盤(pán)彈起時(shí),是否自動(dòng)上推頁(yè)面

受限于設(shè)備系統(tǒng),暫不支持

bindinput

EventHandle

當(dāng)鍵盤(pán)輸入時(shí),觸發(fā) input 事件,event.detail = {value, cursor, keyCode},keyCode 為鍵值。

-

bindfocus

EventHandle

輸入框聚焦時(shí)觸發(fā),event.detail = {value: value, height: height}, height 為鍵盤(pán)高度

-

bindblur

EventHandle

輸入框失去焦點(diǎn)時(shí)觸發(fā),event.detail = {value: value}

-

bindconfirm

EventHandle

點(diǎn)擊完成按鈕時(shí)觸發(fā),event.detail = {value: value}

-

type 有效值

說(shuō)明 Web 態(tài)說(shuō)明

text

文本輸入鍵盤(pán)

-

number

數(shù)字輸入鍵盤(pán)

-

idcard

身份證輸入鍵盤(pán)

受設(shè)備系統(tǒng)或輸入法限制,一些設(shè)備(如,帶原生輸入法的 iOS 設(shè)備)不支持

digit

帶小數(shù)點(diǎn)的數(shù)字鍵盤(pán)

受設(shè)備系統(tǒng)或輸入法限制,一些設(shè)備(如,帶原生輸入法的 iOS 設(shè)備)不支持

confirm-type 有效值

Web 態(tài)說(shuō)明:Web 態(tài)下,受設(shè)備系統(tǒng)或輸入法限制,confirm-type 值無(wú)法修改鍵盤(pán)右下角按鈕文字。右下角按鈕內(nèi)容由設(shè)備系統(tǒng)或輸入法決定。

說(shuō)明

send

鍵盤(pán)右下角按鈕為 “發(fā)送”

search

鍵盤(pán)右下角按鈕為 “搜索”

next

鍵盤(pán)右下角按鈕為 “下一個(gè)”

go

鍵盤(pán)右下角按鈕為 “前往”

done

鍵盤(pán)右下角按鈕為 “完成”

示例 

在開(kāi)發(fā)者工具中打開(kāi)


代碼示例 1: 基礎(chǔ)用法

<view class="wrap">
    <view class="card-area">
        <view class="top-description border-bottom">基礎(chǔ)用法</view>
        <input class="ipt" placeholder="請(qǐng)?jiān)诖溯斎霕?biāo)題" />
    </view>
</view>

代碼示例 2: 自定義輸入控制

<view class="wrap">
    <view class="card-area">
        <view class="top-description border-bottom">自定義輸入控制</view>
        <view class="list-area border-bottom">
            <view class="list-item-key-4">自動(dòng)聚焦</view>
            <view class="list-item-value">
                <input
                    placeholder='focus="true"'
                    selection-start="3"
                    selection-end="7"
                    focus="{{true}}"
                    confirm-hold="false"
                    confirm-type="send"
                    bindfocus="bindKeyfocus" />
            </view>
        </view>

        <view class="list-area border-bottom">
            <view class="list-item-key-4">控制長(zhǎng)度</view>
            <view class="list-item-value">
                <input
                    cursor="100"
                    bindblur="bindKeyblur"
                    bindconfirm="bindKeyconfirm"
                    placeholder='maxlength="10"'
                    maxlength="10" />
            </view>
        </view>

        <view class="list-area border-bottom">
            <view class="list-item-key-4">禁用</view>
            <view class="list-item-value">
                <input disabled="true" placeholder='disabled="true"' />
            </view>
        </view>

        <view class="list-area">
            <view class="list-item-key-4">帶有內(nèi)容</view>
            <view class="list-item-value">
                <input value='value="{= value =}"' />
            </view>
        </view>
    </view>
</view>
Page({
    data: {
        inputValue: '',
        autoFocus: true,
        value: '初始value值'
    },
    onShow(){
        this.setData({
            autoFocus: true
        });
    },
    bindButtonTap: function () {
        this.setData({
            focus: true
        });
    },
    bindKeyInput: function (e) {
        this.setData({
            inputValue: e.detail.value
        });
    },
    bindKeyfocus: function (e){
        console.log(e.detail);
    },
    bindKeyblur: function (e){
        swan.showToast({
            title: '普通input失焦事件',
            icon: 'none'
        });
    },
    bindKeycomfirm: function (e){
        swan.showToast({
            title: '點(diǎn)擊確定',
            icon: 'none'
        });
    }
});

代碼示例 3: 自定義輸入內(nèi)容

<view class="wrap">
    <view class="card-area">
        <view class="top-description border-bottom">自定義輸入內(nèi)容</view>
        <view class="list-area border-bottom">
            <view class="list-item-key-4">文本</view>
            <view class="list-item-value">
                <input
                    type="text"
                    placeholder='type="text"'
                    bindinput="{{!isWeb ? '': 'keyBoardText'}}"
                    value="{= textValue =}" />
            </view>
        </view>

        <view class="list-area border-bottom">
            <view class="list-item-key-4">數(shù)字</view>
            <view class="list-item-value">
                <input type="number" placeholder='type="number"' bindinput="{{!isWeb ? '': 'keyBoardNumber'}}" value="{= numberValue =}" />
            </view>
        </view>

        <view class="list-area border-bottom">
            <view class="list-item-key-4">身份證</view>
            <view class="list-item-value">
                <input type="idcard" adjust-position="true" placeholder='type="idcard"' bindinput="{{!isWeb ? '': 'keyBoardCard'}}" value="{= cardValue =}" />
            </view>
        </view>

        <view class="list-area">
            <view class="list-item-key-4">小數(shù)</view>
            <view class="list-item-value">
            <input type="digit" placeholder='type="digit"' bindinput="{{!isWeb ? '': 'keyBoardDigit'}}" value="{= digitValue =}" />
            </view>
        </view>
    </view>
</view>

代碼示例 4: 自定義占位符顏色

<view class="wrap">
    <view class="card-area">
        <view class="top-description border-bottom">
            <view>自定義占位符顏色</view>
            <view>
                placeholder-style=
                color:"#3388FF"
            </view>
        </view>
        <input class="ipt" placeholder-class="placeholder" placeholder-style="color:#3388FF" placeholder="請(qǐng)?jiān)诖溯斎? />
    </view>
</view>

代碼示例 5: 實(shí)時(shí)獲取輸入值

<view class="wrap">
    <view class="card-area">
        <view class="top-description">
            <view>實(shí)時(shí)獲取輸入值</view>
            <view>bindinput="bindKeyInput"</view>
        </view>
        <view class="textarea">{{inputValue}}</view>
        <input class="ipt" bindinput="bindKeyInput" placeholder="請(qǐng)?jiān)诖溯斎雰?nèi)容" />
    </view>
</view>


以上內(nèi)容是否對(duì)您有幫助:
在線(xiàn)筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)