W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
輸入框,可設(shè)置輸入內(nèi)容的類型、長(zhǎng)度、顯示形式等。當(dāng)用戶需要輸入文字內(nèi)容時(shí)點(diǎn)擊文本框,它將自動(dòng)打開鍵盤。使用文本字段來(lái)請(qǐng)求少量信息。
注意:
<!-- API-DEMO page/component/input/input.axml -->
<view class="page">
<view class="page-description">輸入框</view>
<view class="page-section">
<view class="form-row">
<view class="form-row-label">受控聚焦</view>
<view class="form-row-content">
<input class="input" focus="{{focus}}" onFocus="onFocus" onBlur="onBlur" placeholder="input something" />
</view>
</view>
<view class="page-section-btns">
<button size="mini" onTap="bindButtonTap">聚焦</button>
</view>
</view>
<view class="page-section">
<view class="form-row">
<view class="form-row-label"><label for="controlled">顯示輸入</label></view>
<view class="form-row-content">
<input class="input" id="controlled" onInput="bindKeyInput" placeholder="show input content" />
</view>
</view>
<view class="extra-info">你輸入的是:{{inputValue}}</view>
</view>
<view class="page-section">
<view class="form-row">
<view class="form-row-label">最大長(zhǎng)度</view>
<view class="form-row-content">
<input class="input" maxlength="10" placeholder="maxlength 10" />
</view>
</view>
<view class="form-line" />
<view class="form-row">
<view class="form-row-label">收起鍵盤</view>
<view class="form-row-content">
<input class="input" onInput="bindHideKeyboard" placeholder="輸入 123 自動(dòng)收起鍵盤" />
</view>
</view>
<view class="form-line" />
<view class="form-row">
<view class="form-row-label">輸入密碼</view>
<view class="form-row-content">
<input class="input" password type="text" placeholder="密碼輸入框" />
</view>
</view>
<view class="form-line" />
<view class="form-row">
<view class="form-row-label">輸入數(shù)字</view>
<view class="form-row-content">
<input class="input" type="number" placeholder="數(shù)字輸入框" />
</view>
</view>
<view class="form-line" />
<view class="form-row">
<view class="form-row-label">小數(shù)點(diǎn)鍵盤</view>
<view class="form-row-content">
<input class="input" type="digit" placeholder="帶小數(shù)點(diǎn)的數(shù)字鍵盤" />
</view>
</view>
<view class="form-line" />
<view class="form-row">
<view class="form-row-label">身份證鍵盤</view>
<view class="form-row-content">
<input class="input" type="idcard" placeholder="身份證輸入鍵盤" />
</view>
</view>
</view>
<view class="page-section">
<view class="page-section-title">搜索框</view>
<view class="page-section-demo">
<view class="search-outer">
<input
class="search-input"
placeholder="搜索"
value="{{search}}"
onConfirm="doneSearch"
onInput="handleSearch"
/>
<text class="search-cancel" onTap="clearSearch">取消</text>
</view>
</view>
</view>
</view>
// API-DEMO page/component/input/input.js
Page({
data: {
focus: false,
inputValue: '',
},
bindButtonTap() {
// blur 事件和這個(gè)沖突
setTimeout(() => {
this.onFocus();
}, 100);
},
onFocus() {
this.setData({
focus: true,
});
},
onBlur() {
this.setData({
focus: false,
});
},
bindKeyInput(e) {
this.setData({
inputValue: e.detail.value,
});
},
bindHideKeyboard(e) {
if (e.detail.value === '123') {
// 收起鍵盤
my.hideKeyboard();
}
},
handleSearch(e) {
console.log('search', e.detail.value);
this.setData({
search: e.detail.value,
});
},
doneSearch() {
console.log('doneSearch', this.data.search);
my.hideKeyboard();
},
clearSearch() {
console.log('clear search', this.data.search);
this.setData({
search: '',
});
},
});
/* API-DEMO page/component/input/input.acss */
.extra-info {
border-top: 1px solid #ddd;
margin-left: 30rpx;
padding: 20rpx 0;
overflow: auto;
}
.search-outer {
box-sizing: border-box;
display:flex;
height:40px;
overflow:hidden;
padding: 8px;
border-bottom: 1px solid #ddd;
background-color: #efeff4;
}
.search-outer * {
box-sizing: border-box;
}
.search-input {
flex:1;
text-align: left;
display: block;
color: #000;
height: 24px;
font-size: 15px;
background-color: #fff;
border-color: transparent;
}
.search-input:focus + .search-cancel {
margin-right:0;
opacity: 1;
}
.search-cancel {
margin-right:-40px;
display: inline-block;
opacity: 0;
padding-left: 8px;
height: 24px;
line-height: 24px;
font-size: 16px;
color: #108ee9;
text-align: right;
transition: margin-right .3s,opacity .3s;
transition-delay: .1s;
}
屬性 | 類型 | 默認(rèn)值 | 描述 | 最低版本 |
---|---|---|---|---|
value | String | - | 初始內(nèi)容。 | - |
name | String | - | 組件名字,用于表單提交獲取數(shù)據(jù)。 | - |
type | String | text | input 的類型,有效值:text 、 number 、 idcard 、 digit (可以喚起帶有小數(shù)點(diǎn)的數(shù)字鍵盤)、numberpad 、digitpad 、 idcardpad 。 |
numberpad 、digitpad 、 idcardpad 基礎(chǔ)庫(kù) 1.13.0 客戶端 10.1.50,可通過(guò) my.canIUse(input.type.numberpad)來(lái)檢測(cè)。 |
password | Boolean | false | 是否是密碼類型。 | - |
placeholder | String | - | 占位符。 | - |
placeholder-style | String | - | 指定 placeholder 的樣式,可設(shè)置間距。 | 1.6.0 |
placeholder-class | String | - | 指定 placeholder 的樣式類。 | 1.6.0 |
disabled | Boolean | false | 是否禁用。 | - |
maxlength | Number | 140 | 最大長(zhǎng)度。 | - |
focus | Boolean | false | 獲取焦點(diǎn)。 | - |
confirm-type | String | done | 設(shè)置鍵盤右下角按鈕的文字,有效值:done(顯示“完成”)、go(顯示“前往”)、next(顯示“下一個(gè)”)、search(顯示“搜索”)、send(顯示“發(fā)送”),平臺(tái)不同顯示的文字略有差異。注意:只有在 type=text 時(shí)有效。 | 1.7.0 |
confirm-hold | Boolean | false | 點(diǎn)擊鍵盤右下角按鈕時(shí)是否保持鍵盤不收起狀態(tài)。 | 1.7.0 |
cursor | Number | - | 指定 focus 時(shí)的光標(biāo)位置。 | - |
selection-start | Number | -1 | 獲取光標(biāo)時(shí),選中文本對(duì)應(yīng)的焦點(diǎn)光標(biāo)起始位置,需要和 selection-end 配合使用。 | 1.7.0 |
selection-end | Number | -1 | 獲取光標(biāo)時(shí),選中文本對(duì)應(yīng)的焦點(diǎn)光標(biāo)結(jié)束位置,需要和 selection-start 配合使用。 | 1.7.0 |
random-number | Boolean | false | 當(dāng) type 為 number, digit, idcard 數(shù)字鍵盤是否隨機(jī)排列。 | 1.9.0 |
controlled | Boolean | false | 是否為受控組件。為 true 時(shí),value 內(nèi)容會(huì)完全受 setData 控制。 | 1.8.0 |
onInput | EventHandle | - | 鍵盤輸入時(shí)觸發(fā) input 事件,event.detail = {value: value} 。 |
- |
onConfirm | EventHandle | - | 點(diǎn)擊鍵盤完成時(shí)觸發(fā),event.detail = {value: value} |
- |
onFocus | EventHandle | - | 聚焦時(shí)觸發(fā),event.detail = {value: value} 。 |
- |
onBlur | EventHandle | - | 失去焦點(diǎn)時(shí)觸發(fā)(僅支持真機(jī)),event.detail = {value: value} 。 |
- |
步驟一:若已在 input 中設(shè)置了 enableNative 屬性,刪除 enableNative 屬性的全部設(shè)置。
步驟二:在 app.json 文件 window 對(duì)象內(nèi),設(shè)置 "enableInPageRenderInput":"YES"。
因?yàn)槭褂枚ㄎ粚?dǎo)致鍵盤把頁(yè)面 input 內(nèi)容頂上去了,建議使用 SearchBar 搜索框。
商戶有使用 fixed 定位 建議更換 fixed 定位使用相對(duì)或者絕對(duì)定位。
暫不支持 ,input 是在聚焦的時(shí)候彈起 ,失去焦點(diǎn)的時(shí)候收起,若讓鍵盤處于一直彈起狀態(tài),就要保證 input 一直聚焦。
建議修改自定義 view 樣式。
暫時(shí)不支持,可以考慮外嵌一層 view,利用 view 的 onTap 事件實(shí)現(xiàn)。
需要添加屬性 controlled="{{true}}" ,也可以在 onInput 事件里把輸入的值通過(guò) setData 再賦值給 value,再去 setData 設(shè)置 value。
//axml
<input class="internet_input" value="{{textValue}}" onInput="keyNum" controlled={{true}} type="text" />
//input如何用js清空
keyNum() {
this.setData({
textValue:''
})
}
可以使用 input 的 onInput 事件監(jiān)聽輸入值,通過(guò) e.detail.value 打印出輸入值進(jìn)行正則表達(dá)式匹配校驗(yàn)。詳情請(qǐng)見示例代碼。
使用 var reg = new RegExp("\w+\s", "g"); getRegExp() 需要在 sjs 中使用。 sjs 腳本不能直接在 js 中引入調(diào)用。
請(qǐng)參見 組件對(duì)象。
可以使用防抖動(dòng),示例代碼如下:
var timer = null
element.input = function () {
clearTimeout(timer) // 每次進(jìn)來(lái)的時(shí)候都將之前的清除掉,如果還沒到一秒的時(shí)候就將之前的清除掉,這樣就不會(huì)觸發(fā)之前setTimeout綁定的事件, 如果超過(guò)一秒,之前的事件就會(huì)被觸發(fā)下次進(jìn)來(lái)的時(shí)候同樣清除之前的timer
timer = setTimeout(function () {
// 在這里進(jìn)行我們的操作 這樣就不會(huì)頻繁的進(jìn)行我們這里面的操作了
}, 1000)
}
修改字體顏色,會(huì)有灰蒙顏色的效果,無(wú)法去除。如果只想顯示內(nèi)容,建議使用 view 實(shí)現(xiàn)顯示內(nèi)容。
.extra-info {
border-top: 1px solid #ddd;
margin-left: 30rpx;
padding: 20rpx 0;
overflow: auto;
disabled:true;
color: red
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: