微信小程序 密码框改为text后不可见,需要点击一下

这个问题是做项目的时候碰到的。

密码框常规写法:

复制代码
<view class="inputBox">
  <view class="input-container">
    <input type="{{inputType}}" placeholder="请输入密码" data-id="passwordValue" bindinput="getinput" value="{{passwordValue}}" placeholder-style="color:#ccc8c8" />
    <image class="eye-icon" src="{{isPasswordVisible ? '/images/open-eye.png' : '/images/closed-eye.png'}}" bind:touchstart="onEyeTouchStart" bind:touchend="onEyeTouchEnd"/>
  </view>
</view>

函数:onEyeTouchStart 和 onEyeTouchEnd 的实现:

复制代码
onEyeTouchStart() {
    const passwordValue = this.data.passwordValue;
    this.setData({
      inputType: 'text',
      isPasswordVisible: true
    });
  },
  onEyeTouchEnd() {
    const passwordValue = this.data.passwordValue;
    this.setData({
      inputType: 'password',
      isPasswordVisible: false,
    });
  },

然后就会出现以上的问题(真机上),后来查了不少资料,有个方法可以解决这个问题:

那就是改变input属性后刷新一下,我改进了一下,确实解决了这个问题,特此记录。

改进后的代码:

复制代码
onEyeTouchStart() {
    const passwordValue = this.data.passwordValue;
    this.setData({
      inputType: 'text',
      isPasswordVisible: true,
      passwordValue:''
    },()=>{
      this.setData({
        passwordValue:passwordValue
      });
    });
    console.log('in onEyeTouchStart,inputType:',this.data.inputType,'isPasswordVisible',this.data.isPasswordVisible);
  },
  onEyeTouchEnd() {
    const passwordValue = this.data.passwordValue;
    this.setData({
      inputType: 'password',
      isPasswordVisible: false,
      passwordValue:''
    },()=>{
      this.setData({
        passwordValue:passwordValue
      });
    });
    console.log('in onEyeTouchEnd,inputType:',this.data.inputType,'isPasswordVisible',this.data.isPasswordVisible);
  },
相关推荐
小小王app小程序开发20 小时前
抽赏小程序特殊赏玩法开发全解析:技术实现+架构支撑+合规落地
小程序·架构
江南西肥肥20 小时前
从手绘图到小程序,我用AI花了2个小时完成
小程序·vibecoding·claudecode
柠檬树^-^1 天前
小程序定位
小程序
计算机毕设指导61 天前
基于微信小程序民宿预订管理系统【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven
tjjucheng1 天前
专业小程序定制开发公司推荐
大数据·小程序
莫非技术栈1 天前
我模仿“死了吗“做了一个打卡签到的小程序
小程序
P7Dreamer1 天前
微信小程序处理Range分片视频播放问题:前端调试全记录
前端·微信小程序
2501_915921431 天前
如何在苹果手机上面进行抓包?iOS代理抓包,数据流抓包
android·ios·智能手机·小程序·uni-app·iphone·webview
苦夏木禾1 天前
在微信小程序中,同样的宽度100%,textarea和其他标签的实际宽度不一样
微信小程序·小程序