- 問題
關於小程序input輸入框中的密碼可見與不可見的具體實現。 - 思路
原理:小程序 input 的密碼類型由 “password” 屬性控制,為 true 時是小圓點密碼類型不可見,為 false 時是普通text類型可見。
操作:將顯示隱藏控制圖標定位到輸入框上,通過圖標的點擊事件,控制 input 的 ‘password’ 屬性值。 - 代碼實現
// wxml文件 <view> <input name="password" placeholder='密碼' value="{{password}}" password='{{isPassword}}'></input> <view class="pwdImage"> <image bindtap='showPassword' src="{{isPassword ? '../images/hide.png' : '../images/view.png' }}" mode="aspectFit"></image> </view> </view> // js文件 data: {isPassword: true}, showPassword() { var isPassword = !this.data.isPassword; this.setData({ isPassword: isPassword }) }
- 效果