看到小红通过了它的好友申请,小蓝赶紧发起聊天:小红美眉,你平时喜欢做什么 呀,有空我们可以一起约呀。此处就可以使用 input 输入框啦,不仅可以输入文 字还可以输入数字、密码哦。 输入框,可设置输入内容的类型、长度、显示形式等。当用户需要输入文字内容时 点击文本框,它将自动打开键盘。使用文本字段来请求少量信息。 注意: iOS 系统支付宝客户端版本 10.1.80 及以上不支持 focus=true 自动唤起。 小程序中 input 如果父类是 position: fixed,可以加上 enableNative="{{false}}",解决 输入框错位问题。 示例代码
<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">最大长度</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
自动收起键盘" />
</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">输入数字</view>
<view class="form-row-content">
<input class="input" type="number" placeholder="数字输入框" />
</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="digit" placeholder="带小数点的数字键盘" />
</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>
data: {
focus: false,
inputValue: '',
},
bindButtonTap() {
// blur 事件和这个冲突
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: '',
});
},
});
.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;
}
失去焦点时触发(仅支持真 机),event.detail = {value: value} 内容来源:https://developer.aliyun.com/article/756818?spm=a2c6h.12873581.0.dArticle756818.26162b70Su1GZy&groupCode=tech_library
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。