如题,下面这段代码,如果之前在有用户在网站记住了密码,注册的时候这个框会自动填入,有什么办法禁止在“注册”表单中自动填入内容呢?
在 <form action="" method="post" autocomplete="off">
添加 autocomplete="off"
无效,<input>
添加也无效,Chrome 浏览器里。
我只希望在登录表单能自动输入邮箱和密码:
<form action="" method="post" autocomplete="off">
<input type="email" name="email">
<input type="password" name="password">
<button type="submit">Register</button>
</form>
對,現在許多瀏覽器都會忽略對用戶名密碼的 autocomplete="off"。
autocomplete 本來是瀏覽器私自實現的功能(雖然已幾乎成必備了),所以其行爲也沒有標準規範。
因此,就目前爲止,只要避免瀏覽器將表單識別爲含有用戶名密碼的表單即可。
在 password 之前加一個隱藏的 type="password" 即可。
<form>
<input name="a" autocomplete="off">
<input type="password" style="display:none">
<input type="password" name="b"><button>Sign in</button>
</form>
經測試,此法對 Chrome 和 Safari 有效(Safari 會提示記住密碼,但無法填充,iOS 本來就不會自動填充但手動填充保存的密碼正常)
Firefox 好像本來就不會自動填充(需要手動選擇),所以測試無意義。。。
參見:http://.com/q/1010000000663173
之前项目中也遇到过类似的问题,当时查了一段时间后发现貌似不是规范问题,应该是浏览器的特定优化。比如 chrome 里 input 的 name
属性是 username、email 之类的比较敏感的词它会自动尝试填充,后来就换成了其他的一些不太敏感的词。
<input type="text" name="clear" style="position:fixed;bottom:-9999px;">
<input type="password" name="clear" style="position:fixed;bottom:-9999px;">
使用这个方法可以解决问题
autocomplete="off" 这东西恐怕不是标准的xhtml元素属性,你可以查查html规范去。
要做到这样,你需要配合一点js让它产生作用,类似这样:
if (document.getElementsByTagName) {
var inputElements = document.getElementsByTagName(“input”);//整个doc的input
for (i=0; inputElements[i]; i++) { //遍历设置属性
if (inputElements[i].className && (inputElements[i].className.indexOf(“disableAutoComplete”)!= -1)) {
inputElements[i].setAttribute(“autocomplete”,”off”);
} } }