我遇到了chrome自动填充行为的几个形式的问题。

表单中的字段都有非常常见和准确的名称,例如“email”、“name”或“password”,并且它们还设置了autocomplete=“off”。

自动完成标志已经成功禁用了自动完成行为,当你开始输入时,会出现一个下拉的值,但没有改变Chrome自动填充字段的值。

这种行为是可以的,除了chrome填充输入不正确,例如填充电话输入与电子邮件地址。客户抱怨过这个问题,所以它被证实在很多情况下都发生了,而不是我在我的机器上本地操作的某种结果。

目前我能想到的唯一解决方案是动态生成自定义输入名称,然后在后端提取值,但这似乎是一种相当笨拙的解决这个问题的方法。是否有任何标签或怪癖,改变自动填充行为,可以用来解决这个问题?


当前回答

What I have done it to change the input type="text" to a multi line input ie. overflow-x:hidden; overflow-y:hidden; vertical-align:middle; resize: none; A quick explanation of the code: The overflow-x and -y hidden wil disable the scroll buttons on the right of the textarea box. The vertial algin will align the lable vertical middle with the text area and the resize: none will disable the resize grabber at the bottom right of the textarea. In essance it means that your textarea will appear like a textbox, but with chrome autofill off.

其他回答

2016年谷歌Chrome开始忽略autocomplete=off,尽管它在W3C中。他们发布的答案是:

这里棘手的部分是,在web的某个地方,autocomplete=off成为许多表单字段的默认值,而没有真正考虑这是否对用户有好处。这并不意味着不存在不希望浏览器自动填充数据的有效情况(例如在CRM系统上),但总的来说,我们认为这些是少数情况。因此,我们开始忽略autocomplete=off的Chrome自动填充数据。

本质上说:我们更了解用户想要什么。

当需要autocomplete=off时,他们打开了另一个错误来发布有效的用例

在我所有的B2B应用程序中,我还没有看到与自动完成相关的问题,但只在输入密码类型时出现。

如果屏幕上有任何密码字段,即使是隐藏的,自动填充步骤。 要打破这种逻辑,如果不打破自己的页面逻辑,可以将每个密码字段放入自己的表单中。

<input type=name >

<form>
    <input type=password >
</form>

通过这个技巧,自动完成功能已经成功禁用。 它的工作原理!

[HTML]

<div id="login_screen" style="min-height: 45px;">
   <input id="password_1" type="text" name="password">
</div>

(JQuery)

$("#login_screen").on('keyup keydown mousedown', '#password_1', function (e) {
    let elem = $(this);

    if (elem.val().length > 0 && elem.attr("type") === "text") {
        elem.attr("type", "password");
    } else {
        setTimeout(function () {
            if (elem.val().length === 0) {
                elem.attr("type", "text");
                elem.hide();
                setTimeout(function () {
                    elem.show().focus();
                }, 1);
            }
        }, 1);
    }

    if (elem.val() === "" && e.type === "mousedown") {
        elem.hide();
        setTimeout(function () {
            elem.show().focus();
        }, 1);
    }

});

Mike Nelsons提供的解决方案在Chrome 50.0.2661.102 m中不工作。 简单地添加一个与display:none设置相同类型的输入元素不再禁用本机浏览器自动完成功能。现在需要复制希望禁用自动完成的输入字段的name属性。

此外,为了避免输入字段在表单元素中重复,应该在不显示的元素上放置一个disabled。这将阻止该元素作为表单操作的一部分提交。

<input name="dpart" disabled="disabled" type="password" style="display:none;">
<input name="dpart" type="password">
<input type="submit">

如果表单是通过javascript生成的,那么通过在输入中添加“display: none;”来隐藏它的方法对我来说就行不通了。

所以我把它们放在视线之外,让它们隐形:

<input style="width:0;height:0;opacity:0;position:absolute;left:-10000px;overflow:hidden;" type="text" name="fakeusernameremembered"/>
<input style="width:0;height:0;opacity:0;position:absolute;left:-10000px;overflow:hidden;" type="password" name="fakepasswordremembered"/>

我的工作,因为上面没有一个似乎工作在Chrome 63及以上

我在我的网站上通过替换违规的输入元素来解决这个问题

<p class="input" contenteditable="true">&nbsp;</p>

在提交之前使用jQuery填充隐藏字段。

但这确实是一个可怕的黑客,因为铬的一个糟糕的决定。