如何在主要浏览器中禁用特定输入(或表单字段)的自动完成?
当前回答
多年来我一直在为自动完成而奋斗。我尝试了每一个建议,但都没有奏效。通过jQuery 2添加属性效果良好:
$(document).ready( function () {
setTimeout(function() {
$('input').attr('autocomplete', 'off').attr('autocorrect', 'off');
}, 10);
});
将生成HTML
<input id="start_date" name="start_date" type="text" value="" class="input-small hasDatepicker" autocomplete="off" placeholder="Start date" autocorrect="off">
其他回答
为了解决这个问题,我使用了一些CSS技巧和以下工作。
input {
text-security:disc;
-webkit-text-security:disc;
-mox-text-security:disc;
}
请阅读本文了解更多详情。
在这次谈话中,没有一个解决方案对我有效。
我终于找到了一个纯HTML解决方案,它不需要任何JavaScript,可以在现代浏览器中工作(除了Internet Explorer;至少要有一个捕获,对吗?),并且不需要对整个表单禁用自动完成。
只需关闭表单上的自动完成功能,然后为您希望在表单中工作的任何输入打开它。例如:
<form autocomplete="off">
<!-- These inputs will not allow autocomplete and Chrome
won't highlight them yellow! -->
<input name="username" />
<input name="password" type="password" />
<!-- This field will allow autocomplete to work even
though we've disabled it on the form -->
<input name="another_field" autocomplete="on" />
</form>
有时,即使autocomplete=off也无法阻止将凭据填写到错误的字段中,而不是用户或昵称字段。
除了apinstein关于浏览器行为的帖子之外,还有一个变通方法。
修复浏览器自动填充为只读,并在焦点上设置为可写(单击并选项卡)
<input type="password" readonly
onfocus="this.removeAttribute('readonly');"/>
更新:
Mobile Safari在字段中设置光标,但不显示虚拟键盘。新的修复程序与以前一样,但它处理虚拟键盘:
<input id="email" readonly type="email" onfocus="if (this.hasAttribute('readonly')) {
this.removeAttribute('readonly');
// fix for mobile safari to show virtual keyboard
this.blur(); this.focus(); }" />
现场演示https://jsfiddle.net/danielsuess/n0scguv6/
//更新结束
因为浏览器自动将凭据填充到错误的文本字段!?
我注意到在Chrome和Safari上,当相同的表单中有密码字段时,会出现这种奇怪的行为。我猜浏览器会寻找一个密码字段来插入您保存的凭据。然后它会自动填充(只是由于观察而猜测)最近的类似文本的输入字段,该字段出现在DOM中的密码字段之前。因为浏览器是最后一个实例,您无法控制它。
上面的这个只读修复对我有用。
Chrome的解决方案是在输入类型密码中添加autocomplete=“new password”。请检查以下示例。
例子:
<form name="myForm"" method="post">
<input name="user" type="text" />
<input name="pass" type="password" autocomplete="new-password" />
<input type="submit">
</form>
如果Chrome找到一个密码类型的框,它总是自动完成数据,这足以表明该框的自动完成=“新密码”。
这对我很有用。
注意:使用F12确保更改生效。很多时候,浏览器都会将页面保存在缓存中,这给我一个不好的印象,认为它不起作用,但浏览器实际上并没有带来更改。
没有虚假输入,没有javascript!
无法在浏览器之间一致禁用自动填充。我尝试了所有不同的建议,但没有一个在所有浏览器中都有效。唯一的方法是根本不使用密码输入。下面是我想到的:
<style type="text/css">
@font-face {
font-family: 'PasswordDots';
src: url('text-security-disc.woff') format('woff');
font-weight: normal;
font-style: normal;
}
input.password {
font-family: 'PasswordDots' !important;
font-size: 8px !important;
}
</style>
<input class="password" type="text" spellcheck="false" />
下载:text-security-disc.woff
以下是我的最终结果:
负面的副作用是,可以从输入中复制纯文本,但使用一些JS应该可以防止这种情况。