微信小程序 密码框改为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);
  },
相关推荐
Q_Q5110082852 小时前
python+uniapp基于微信小程序的心理咨询信息系统
spring boot·python·微信小程序·django·flask·uni-app·node.js
HuYi_code3 小时前
WeChat 小程序下载文件实现
微信小程序·uni-app
00后程序员张4 小时前
HTTPS 包 抓取与分析实战,从抓包到解密、故障定位与真机取证
网络协议·http·ios·小程序·https·uni-app·iphone
一匹电信狗4 小时前
【C++】C++风格的类型转换
服务器·开发语言·c++·leetcode·小程序·stl·visual studio
2501_915921434 小时前
iOS混淆与IPA加固实战手记,如何构建苹果应用防反编译体系
android·macos·ios·小程序·uni-app·cocoa·iphone
Q_Q5110082857 小时前
python+uniapp基于微信小程序的学院设备报修系统
spring boot·python·微信小程序·django·flask·uni-app
李慕婉学姐7 小时前
【开题答辩过程】以《自习室预约微信小程序》为例,不会开题答辩的可以进来看看
微信小程序·小程序
LB21127 小时前
苍穹外卖-购物车 前端修改(小程序主页与购物车模块显示不一致)
小程序
peachSoda77 小时前
自定义配置小程序tabbar逻辑思路
javascript·vue.js·微信小程序·小程序
ღ᭄ꦿ࿐Never say never꧂9 小时前
微信小程序 Button 组件 open-type 完全指南:从用户信息获取到客服分享的实战应用
spring boot·微信小程序·小程序·uni-app·vue3