如何在主要浏览器中禁用特定输入(或表单字段)的自动完成?


当前回答

Chrome一直试图强制自动完成。所以你需要做一些事情。

输入字段的属性id和名称不需要是可识别的。所以没有姓名、电话、地址等值。

相邻的标签或div也需要不可识别。然而,您可能会想,用户如何知道?好吧,在使用内容后,您可以使用::做一个有趣的小技巧。你可以将其设置为字母。

<label>Ph<span class='o'></span>ne</label>
<input id='phnbr' name='phnbr' type='text'>

<style>
  span.o {
    content: 'o';
  }
</style>

其他回答

除了

autocomplete="off"

使用

readonly onfocus="this.removeAttribute('readonly');"

对于不希望他们记住表单数据(用户名、密码等)的输入,如下所示:

<input type="text" name="UserName" autocomplete="off" readonly 
    onfocus="this.removeAttribute('readonly');" >

<input type="password" name="Password" autocomplete="off" readonly 
    onfocus="this.removeAttribute('readonly');" >

我遇到了同样的问题,今天2019年10月9日,我找到的唯一解决方案是

将autocomplete=“off”添加到表单标记中。

打开表单标签后输入1个错误。

<input id="username" style="display:none" type="text" name="fakeusernameremembered">

但它在密码类型字段上不起作用,请尝试

<input type="text" oninput="turnOnPasswordStyle()" placeholder="Enter Password" name="password" id="password" required>

在脚本上

function turnOnPasswordStyle() {
    $('#password').attr('type', "password");
}

这在Chrome-78、IE-44、Firefox-69上进行了测试

Firefox 30忽略密码的autocomplete=“off”,而是选择提示用户密码是否应存储在客户端上。请注意2014年5月5日的以下评论:

密码管理器始终提示是否要保存密码。未经用户许可,密码不会保存。我们是继IE和Chrome之后第三个实现这一改变的浏览器。

根据Mozilla开发者网络文档,布尔表单元素属性autocomplete防止表单数据在旧浏览器中缓存。

<input type="text" name="foo" autocomplete="off" />

添加

autocomplete=“关闭”

表单标记将禁用浏览器对该特定表单中所有输入字段的自动完成(之前在该字段中键入的内容)。

测试日期:

Firefox 3.5,4测试版Internet Explorer 8铬

我刚刚遇到了这个问题,并尝试了几次失败,但这一次对我有效(在MDN上找到):

在某些情况下,浏览器将继续建议自动完成值即使autocomplete属性设置为off。这是意外的对于开发人员来说,这种行为可能非常令人困惑。真正的诀窍强制无完成是将随机字符串分配给属性像这样:

autocomplete="nope"