title: textarea 多行输入框 header: develop nav: component sidebar: formlist_textarea

webUrl: https://qft12m.smartapps.cn/component/textarea/textarea

解释:多行输入框。客户端创建的原生组件,不支持嵌套在其它组件中使用。

属性说明

属性名 类型 默认值 必填 说明 最低支持版本 Web 态说明
value String 输入框的内容,若要动态设置输入框内容,需设置 value="{= value =}"(注: 若要取键盘输入后的value请通过bindinput获取) - -
disabled Boolean false 是否禁用 - -
maxlength Number 140 最大输入长度,设置为 -1 的时候不限制最大长度 - -
placeholder String 输入框为空时占位符 - -
placeholder-style String 指定 placeholder 的样式 - -
placeholder-class String 指定 placeholder 的样式类 - -
auto-height Boolean false 是否自动增高,设置auto-height时,style.height不生效 - -
cursor Number -1 指定focus时的光标位置 - -
auto-focus Boolean false 自动聚焦,调起键盘。 - -
confirm-type String default 设置键盘右下角按钮的文字。 3.70.53

低版本请做兼容性处理

暂不支持
confirm-hold Boolean false 点击键盘右下角按钮时是否保持键盘不收起 3.130.1

低版本请做兼容性处理

-
focus Boolean false 获取焦点,调起键盘 - -
fixed Boolean false 如果 textarea 是在一个 position:fixed 的区域,需要显示指定属性 fixed 为 true - -
cursor-spacing Number 0 指定光标与键盘的距离,单位 px 。取 textarea 距离底部的距离和 cursor-spacing 指定的距离的最小值作为光标与键盘的距离 - -
show-confirm-bar Boolean true 是否显示键盘上方带有”完成“按钮那一栏。 - 暂不支持
selection-start Number -1 光标起始位置,自动聚焦时有效,需与selection-end搭配使用 - -
selection-end Number -1 光标结束位置,自动聚焦时有效,需与selection-start搭配使用 - -
adjust-position Boolean true 键盘弹起时,是否自动上推页面 - 受限于设备系统,暂不支持
bindfocus EventHandle 输入框聚焦时触发,event.detail = { value, height },height为键盘高度 - -
bindblur EventHandle 输入框失去焦点时触发,event.detail = {value, cursor} - -
bindlinechange EventHandle 输入框行数变化时调用,event.detail = {height: 0, heightRpx: 0, lineCount: 0, lineHeight: 0} - -
bindinput EventHandle 当键盘输入时,触发 input 事件,event.detail = {value, cursor}, bindinput 处理函数的返回值并不会反映到 textarea 上 - -
bindconfirm EventHandle 点击完成时, 触发 confirm 事件,event.detail = {value: value} - -

confirm-type 有效值

说明
default 原生键盘状态,输入状态下右下角按钮为“确认”,可将用户正在输入的文字填充至输入框,未输入状态下右下角按钮为“换行”,用户点击后可手动换行
done 右下角按钮为“完成”,点击会触发bindconfirm事件
send 右下角按钮为“发送”,点击会触发bindconfirm事件
search 右下角按钮为“搜索”,点击会触发bindconfirm事件
next 右下角按钮为“下一步”,点击会触发bindconfirm事件
go 右下角按钮为“前往”,点击会触发bindconfirm事件

示例

在开发者工具中预览效果

扫码体验

webUrl: https://qft12m.smartapps.cn/component/textarea/textarea - 图1 请使用百度APP扫码

代码示例 1:输入区高度自适应

在开发者工具中预览效果

:::codeTab

  1. <view class="wrap">
  2. <view class="description">输入区高度自适应</view>
  3. <textarea auto-height maxlength="-1" bindinput="bindInput"/>
  4. </view>
  1. Page({
  2. data: {
  3. height: 1,
  4. focus: true
  5. },
  6. bindfocus(e) {
  7. console.log('focus - e:', e);
  8. },
  9. bindInput(e) {
  10. console.log('input - e:', e);
  11. },
  12. bindLineChange(e) {
  13. console.log('linechange - e:', e);
  14. },
  15. bindblur(e) {
  16. console.log('blur - e:', e);
  17. },
  18. bindConfirm(e){
  19. console.log('confirm - e:', e);
  20. }
  21. });

:::

代码示例 2: 受控聚焦

在开发者工具中预览效果

:::codeTab

  1. <view class="wrap">
  2. <view class="description">受控聚焦</view>
  3. <textarea style="height: 3em"
  4. maxlength="-1"
  5. disabled="{{false}}"
  6. auto-focus="{{true}}"
  7. placeholder="我会出现滚动条~"
  8. placeholder-class="plh"
  9. cursor="-1"
  10. confirm-type="send"
  11. confirm-hold="{{false}}"
  12. fixed="{{true}}"
  13. focus="{{focus}}"
  14. cursor-spacing="20"
  15. show-confirm-bar="{{true}}"
  16. selection-start="-1"
  17. selection-end="-1"
  18. adjust-position="{{true}}"
  19. bindfocus="bindFocus"
  20. bindblur="bindBlur"
  21. bindlinechange="bindLineChange"
  22. bindinput="bindInput"
  23. bindconfirm="bindConfirm"
  24. />
  25. <button type="primary" bind:tap="bindFocusTap">聚焦</button>
  26. </view>
  1. Page({
  2. data: {
  3. height: 1,
  4. focus: true
  5. },
  6. onHide() {
  7. getApp().globalData.openParams = ''
  8. },
  9. bindFocusTap() {
  10. this.setData('focus', true);
  11. },
  12. bindfocus(e) {
  13. console.log('focus - e:', e);
  14. },
  15. bindInput(e) {
  16. console.log('input - e:', e);
  17. },
  18. bindLineChange(e) {
  19. console.log('linechange - e:', e);
  20. },
  21. bindblur(e) {
  22. console.log('blur - e:', e);
  23. },
  24. bindConfirm(e){
  25. console.log('confirm - e:', e);
  26. }
  27. });

:::

Bug & Tip

  • Tip:textarea 的 blur 事件会晚于页面上的 tap 事件,如果需要在 button 的点击事件获取 textarea,可以使用 form 的 bindsubmit。
  • Tip:不建议在多行文本上对用户的输入进行修改,所以 textarea 的 bindinput 处理函数并不会将返回值反映到 textarea 上。
  • Tip:请使用cover-view组件在 textarea 组件上开发遮罩层。