禁止浏览器自动填充用户名和密码

​ 今天在项目中遇到了一个非常诡异的情况,在页面上点击修改密码后,主页面的分页组件中的页码跳转input框会自动填充当前登录用户名,经过多番查询和尝试,找到了问题的根本原因,浏览器对于用保存过的户名和密码有自动填充功能,关掉自动填充功能成功解决上述问题。

  • 方法一:设置autocompleteoff,适用于普通文本框

    //用户名
    <input type="text" autoComplete="off"/>
    
  • 方法二:设置autocompletenew-password,适用于密码输入框

    //密码
    <input type="password" autoComplete="new-password"/>
    

    需要注意的是在JSX中使用时,需注意大小写问题,必须为autoComplete,否则无效。

  • 方法三:针对密码自动填充,需添加不显示的input且type等于password

    //用户名
    <input type="text" style="position:absolute;z-index:-999"/>
    或者
    <input type="text" style="display:none"/>
    //密码
    <input type="password" style="position:absolute;z-index:-999"/>
    或者
    <input type="password" style="display:none"/>
    
发布了58 篇原创文章 · 获赞 85 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/codezha/article/details/90752488