微信小程序 密码框改为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);
  },
相关推荐
如果你好20 小时前
UniApp 路由导航守卫
前端·微信小程序
大尚来也1 天前
小程序怎么开发自己的小程序
微信小程序
码云数智-园园1 天前
小程序开发平台有哪些?小程序第三方开发平台评测对比
微信小程序
说私域1 天前
流量裂变与数字重塑:基于AI智能名片小程序的短视频全域引流范式研究
人工智能·小程序·流量运营·私域运营
蓝帆傲亦1 天前
支付宝小程序性能暴增秘籍:UniApp项目极限优化全攻略
小程序·uni-app
CHU7290352 天前
淘宝扭蛋机抽盒小程序前端功能解析:解锁趣味抽盒新体验
前端·小程序
2501_933907212 天前
深圳本凡科技专业企业APP开发,助力手机应用创新优化
科技·微信小程序·小程序
每天都要加油呀!2 天前
TypeError: uni.requestPayment is not a function
小程序
java1234_小锋2 天前
分享一套优质的微信小程序校园志愿者系统(SpringBoot后端+Vue3管理端)
微信小程序·小程序·校园志愿者
2501_916008892 天前
深入解析iOS机审4.3原理与混淆实战方法
android·java·开发语言·ios·小程序·uni-app·iphone