课程表

微信小程序框架

微信小程序组件

微信小程序API

微信小程序开放接口

微信小程序工具

微信小程序设计规范

工具箱
速查手册

小程序 表单组件 input

当前位置:免费教程 » 移动开发 » 微信小程序
属性名 类型 默认值 说明 最低版本
value String   输入框的初始内容  
type String "text" input 的类型  
password Boolean false 是否是密码类型  
placeholder String   输入框为空时占位符  
placeholder-style String   指定 placeholder 的样式  
placeholder-class String "input-placeholder" 指定 placeholder 的样式类  
disabled Boolean false 是否禁用  
maxlength Number 140 最大输入长度,设置为 -1 的时候不限制最大长度  
cursor-spacing Number 0 指定光标与键盘的距离,单位 px 。取 input 距离底部的距离和 cursor-spacing 指定的距离的最小值作为光标与键盘的距离  
auto-focus Boolean false (即将废弃,请直接使用 focus )自动聚焦,拉起键盘  
focus Boolean false 获取焦点  
confirm-type String "done" 设置键盘右下角按钮的文字 1.1.0
confirm-hold Boolean false 点击键盘右下角按钮时是否保持键盘不收起 1.1.0
bindinput EventHandle   当键盘输入时,触发input事件,event.detail = {value: value},处理函数可以直接 return 一个字符串,将替换输入框的内容。  
bindfocus EventHandle   输入框聚焦时触发,event.detail = {value: value}  
bindblur EventHandle   输入框失去焦点时触发,event.detail = {value: value}  
bindconfirm EventHandle   点击完成按钮时触发,event.detail = {value: value}  

 

type 有效值:

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

confirm-type 有效值:

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

 

示例代码:下载

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

input

Bug & Tip

  1. bug : 微信版本6.3.30, focus 属性设置无效;
  2. bug : 微信版本6.3.30, placeholder 在聚焦时出现重影问题;
  3. tip : input 组件是一个 native 组件,字体是系统字体,所以无法设置 font-family;
  4. tip : 在 input 聚焦期间,避免使用 css 动画;
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号