微信小程序6位或多位验证码/密码输入框

在做小程序过程中做一个6位验证码输入框,本以为很简单,但是在写的时候遇到各种各样的阻力,在网上查阅资料也寥寥无几,后来经过一番思考,终于敲定下来本人最满意的方案,特意发出来让大家参考一下,希望能帮到大家!

一、效果图如下:



二、代码部分

wxml:

  1. <form bindsubmit="formSubmit">  
  2.   
  3.   <view class='content'>  
  4.   
  5.     <block wx:for="{{Length}}" wx:key="item">  
  6.       <input class='iptbox' value="{{Value.length>=index+1?Value[index]:''}}" disabled password='{{ispassword}}' catchtap='Tap'></input>  
  7.     </block>  
  8.   
  9.   </view>  
  10.   
  11.   <input name="password" password="{{true}}" class='ipt' maxlength="{{Length}}" focus="{{isFocus}}" bindinput="Focus"></input>  
  12.   
  13.   <view>  
  14.     <button class="btn-area" formType="submit">Submit</button>  
  15.   </view>  
  16.   
  17. </form>  

js:

[javascript]  view plain  copy
  1. Page({  
  2.   
  3.   /** 
  4.    * 页面的初始数据 
  5.    */  
  6.   data: {  
  7.     Length:6,        //输入框个数  
  8.     isFocus:true,    //聚焦  
  9.     Value:"",        //输入的内容  
  10.     ispassword:true//是否密文显示 true为密文, false为明文。  
  11.   },  
  12.   Focus(e){  
  13.     var that = this;  
  14.     console.log(e.detail.value);  
  15.     var inputValue = e.detail.value;  
  16.     that.setData({  
  17.       Value:inputValue,  
  18.     })  
  19.   },  
  20.   Tap(){  
  21.     var that = this;  
  22.     that.setData({  
  23.       isFocus:true,  
  24.     })  
  25.   },  
  26.   formSubmit(e){  
  27.     console.log(e.detail.value.password);  
  28.   },  
  29. })  

wxss:

[css]  view plain  copy
  1. .content{  
  2.   display: flex;  
  3.   justify-content: space-around;  
  4.   align-items: center;  
  5.   margin-top200rpx;  
  6. }  
  7. .iptbox{  
  8.   width80rpx;  
  9.   height80rpx;  
  10.   border:1rpx solid #ddd;  
  11.   border-radius: 20rpx;  
  12.   display: flex;  
  13.   justify-contentcenter;  
  14.   align-items: center;  
  15.   text-aligncenter;  
  16. }  
  17. .ipt{  
  18.   width0;  
  19.   height0;  
  20. }  
  21. .btn-area{  
  22.   width80%;  
  23.   background-color: orange;  
  24.   color:white;  
  25. }  

三、思路:

1、放置一个输入框,隐藏其文字和位置,同时设置支付输入框(表格)样式
2、当点击输入框时设置输入框为聚焦状态,唤起键盘,点击空白处,失去焦点,设为失去焦点样式,因为输入框宽高为0,所以不会显示输入框和光标,实现隐藏。
3、限制输入框最大字数并且监听输入框状态,以输入框值的长度作为输入框(表格)内容的渲染条件
4、点击提交按钮时,获取输入框内容。

猜你喜欢

转载自blog.csdn.net/weixin_41871290/article/details/80512600
今日推荐