随笔 - 65  文章 - 0  评论 - 5  阅读 - 17万

解决element-ui input type=password的情况下浏览器自动填充密码问题

前言,当input的type=password,并且前面同时存在一个输入框的情况下,浏览器会出现记录账号密码的弹窗,选择保存记录后下一次会直接填充账号密码框,出现一些前端无法控制的情况,此次修改禁止出现密码自动填充以及选择下拉填充

 

 <el-input v-if="showPass" v-model="loginForm.password" size="small" @change="handlePreLogin" placeholder="请输入密码" :type="((newPwdReadOnly && loginForm.password) || loginForm.password)?'password':'text'" auto-complete="new-password" name="person.user.new_password" @focus="newPwdFocus($event)" :readonly="newPwdReadOnly" @blur="newPwdBlur($event)" ref="newPwdRef">

第一,给各输入项加了auto-complete属性,只能避免保存的时候弹出是否保存密码弹框的问题

内容为空的时候点击type="password"的input框或二次(多次)点击,还是会弹出密码框,所以在上面加了type在password和text之间切换(只能解决第一次多次点击的场景)。
具体解决,给type="password"的输入项增加focus,blur事件和readonly属性,具体focus, blur方法:
 newPwdFocus(evt, isNew = true) {
      if (evt) {
        evt.stopPropagation();
        evt.preventDefault();
      }
      setTimeout(() => {
        if (isNew) {
          this.newPwdReadOnly = false;
        } else {
          this.rePwdReadOnly = false;
        }
      }, 100);
    },
 
newPwdBlur(evt, isNew = true) {
      if (evt) {
        evt.stopPropagation();
      }
      if (isNew) {
        this.newPwdReadOnly = true;
      } else {
        this.rePwdReadOnly = true;
      }
    },
 
关键在于setTimeout 0的延时。
以上还不算完全解决,输入内容,再回车删除内容,发现自动填充框又出来了,所以需要watch以下:
 
watch: {
    "loginForm.password": function () {
      if (this.loginForm.password === "") {
        this.newPwdReadOnly = true;
        this.newPwdFocus(null);
      }
    },
    "loginForm.confirmPwd": function () {
      if (this.loginForm.confirmPwd === "") {
        this.rePwdReadOnly = true;
        this.newPwdFocus(null, false);
      }
    }
 
以上还不算完全解决,内容为空的时候点击type="password"的input框或二次(多次)点击,还是会弹出密码框。或者输入密码,回退清空再点击还是会弹出自动填充框。解决办法,加mousedown事件(注意不是keydown,也不是click)
addClickEvt() {
      if (this.$refs.newPwdRef) {
        this.$refs.newPwdRef.$refs.input.onmousedown =  (evt) => {
          if (evt) {
            evt.preventDefault();
            evt.stopPropagation();
          }
          if (this.loginForm.password === "" || this.newPwdReadOnly) {
            this.$refs.newPwdRef.$refs.input.blur();
            setTimeout(() => {
              this.$refs.newPwdRef.$refs.input.focus();
            }, 0);
          }
          return false;
        };
      }
      if (this.$refs.reNewPwdRef) {
        this.$refs.reNewPwdRef.$refs.input.onmousedown =  (evt) => {
          if (evt) {
            evt.preventDefault();
            evt.stopPropagation();
          }
          if (this.loginForm.confirmPwd === "" || this.rePwdReadOnly) {
            this.$refs.reNewPwdRef.$refs.input.blur();
            setTimeout(() => {
              this.$refs.reNewPwdRef.$refs.input.focus();
            }, 0);
          }
          return false;
        };
      }
    },
 
当点击(或多次点击)密码框的时候会触发mousedown事件,先失焦就阻止了自动填充框的弹出,再聚焦就实现了鼠标还在输入框的功能。
 
posted on   万能的李大少  阅读(2907)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
历史上的今天:
2019-08-02 jquery的append追加的元素,按照class查找的元素的点击事件失效
2019-08-02 移动端iOS中input输入框搜索框软键盘出现换行而不是搜索
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示