输入框的autocomplete为new-password无效?
我为一个输入框设置了autocomplete为new-password,但是浏览器仍然会自动填充用户名到该输入框中,请问该怎么解决?
回复
1个回答

test
2024-08-11
浏览器自动填充行为有时会忽略 autocomplete="new-password" 的设置,尤其是在某些浏览器或特定情况下。这里有几种方法可以尝试解决这个问题:
更改 autocomplete 属性的值:尝试使用不同的值,如 off,虽然这不总是有效,因为不同的浏览器对这些值的支持不同。
<input type="text" name="user" autocomplete="off">
动态设置属性:在页面加载后,通过JavaScript动态设置输入框的 autocomplete 属性,有时可以绕过浏览器的自动填充。
<input type="text" id="username"> document.getElementById('username').setAttribute('autocomplete', 'new-password');
使用隐藏的输入框:在目标输入框之前放置一个或多个隐藏的输入框,这些输入框也设置为 autocomplete="new-password",有时可以“欺骗”浏览器自动填充这些隐藏的输入框而不是实际的输入框。
<input type="password" style="display:none" autocomplete="new-password"> <input type="text" name="user" autocomplete="new-password">
更改输入框的名称和ID:不使用常见的字段名称(如 username, user 等),因为浏览器可能会识别这些名称并尝试自动填充。
<input type="text" name="unique_user_name" id="unique_user_id" autocomplete="new-password">
回复

适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容