input

输入框。

当点击 <form/> 表单中 formType 为 submit 的 <button/> 组件时,会将表单组件中的 value 值进行提交,需要在表单组件中加上 name 来作为 key。

属性名类型默认值说明
valueString输入框的初始内容
typeString"text"input 的类型
passwordBooleanfalse是否是密码类型
placeholderString输入框为空时占位符
placeholder-styleString指定 placeholder 的样式
placeholder-classString"input-placeholder"指定 placeholder 的样式类
disabledBooleanfalse是否禁用
maxlengthNumber140最大输入长度,设置为 -1 的时候不限制最大长度
cursor-spacingNumber0指定光标与键盘的距离,单位 px 。取 input 距离底部的距离和 cursor-spacing 指定的距离的最小值作为光标与键盘的距离
auto-focusBooleanfalse(即将废弃,请直接使用 focus 自动聚焦,拉起键盘
focusBooleanfalse获取焦点
confirm-typeString"done"设置键盘右下角按钮的文字,仅在type='text'时生效
confirm-holdBooleanfalse点击键盘右下角按钮时是否保持键盘不收起
cursorNumber指定 focus 时的光标位置
selection-startNumber-1光标起始位置,自动聚集时有效,需与selection-end搭配使用
selection-endNumber-1光标结束位置,自动聚集时有效,需与selection-start搭配使用
adjust-positionBooleantrue键盘弹起时,是否自动上推页面
bindinputEventHandle键盘输入时触发,event.detail = {value, cursor, keyCode},keyCode 为键值,处理函数可以直接 return 一个字符串,将替换输入框的内容。
bindfocusEventHandle输入框聚焦时触发,event.detail = { value, height },height 为键盘高度
bindblurEventHandle输入框失去焦点时触发,event.detail = {value: value}
bindconfirmEventHandle点击完成按钮时触发,event.detail = {value: value}

type 有效值

说明
text文本输入键盘
number数字输入键盘
idcard身份证输入键盘
digit带小数点的数字键盘

confirm-type 有效值:

说明
send右下角按钮为“发送”
search右下角按钮为“搜索”
next右下角按钮为“下一个”
go右下角按钮为“前往”
done右下角按钮为“完成”

  • 注:confirm-type 的最终表现与手机输入法本身的实现有关,部分安卓系统输入法和第三方输入法可能不支持或不完全支持。
示例代码

  1. <!--input.jxml-->
  2. <view class="page-section">
  3. <input placeholder="这是一个可以自动聚焦的input" auto-focus/>
  4. </view>
  5. <view class="page-section">
  6. <input placeholder="这个只有在按钮点击的时候才聚焦" focus="{{focus}}" />
  7. <view class="btn-area">
  8. <button bindtap="bindButtonTap">使得输入框获取焦点</button>
  9. </view>
  10. </view>
  11. <view class="page-section">
  12. <input maxlength="10" placeholder="最大输入长度10" />
  13. </view>
  14. <view class="page-section">
  15. <view class="page-section-title">你输入的是:{{inputValue}}</view>
  16. <input bindinput="bindKeyInput" placeholder="输入同步到view中"/>
  17. </view>
  18. <view class="page-section">
  19. <input bindinput="bindReplaceInput" placeholder="连续的两个1会变成2" />
  20. </view>
  21. <view class="page-section">
  22. <input password type="number" />
  23. </view>
  24. <view class="page-section">
  25. <input password type="text" />
  26. </view>
  27. <view class="page-section">
  28. <input type="digit" placeholder="带小数点的数字键盘"/>
  29. </view>
  30. <view class="page-section">
  31. <input type="idcard" placeholder="身份证输入键盘" />
  32. </view>
  33. <view class="page-section">
  34. <input placeholder-style="color:red" placeholder="占位符字体是红色的" />
  35. </view>
  1. //input.js
  2. Page({
  3. data: {
  4. focus: false,
  5. inputValue: ''
  6. },
  7. bindButtonTap: function() {
  8. this.setData({
  9. focus: true
  10. })
  11. },
  12. bindKeyInput: function(e) {
  13. this.setData({
  14. inputValue: e.detail.value
  15. })
  16. },
  17. bindReplaceInput: function(e) {
  18. var value = e.detail.value
  19. var pos = e.detail.cursor
  20. if(pos != -1){
  21. //光标在中间
  22. var left = e.detail.value.slice(0,pos)
  23. //计算光标的位置
  24. pos = left.replace(/11/g,'2').length
  25. }
  26. //直接返回对象,可以对输入进行过滤处理,同时可以控制光标的位置
  27. return {
  28. value: value.replace(/11/g,'2'),
  29. cursor: pos
  30. }
  31. //或者直接返回字符串,光标在最后边
  32. //return value.replace(/11/g,'2'),
  33. }
  34. })

input - 图1

Tips

  1. tip : input 组件是一个 native 组件,字体是系统字体,所以无法设置 font-family;
  2. tip : 在 input 聚焦期间,避免使用 css 动画;
  3. tip : 对于将 input 封装在自定义组件中、而 from 在自定义组件外的情况, form 将不能获得这个自定义组件中 input 的值。此时需要使用自定义组件的 内置 behaviors jd://form-field。