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

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

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

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

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


当前回答

我知道这不是完全相关的,但这是我所做的。 自动填充字段会引发一个“change”事件,但前提是你尽可能早地将其绑定到它们。

我把这个放到head部分。

  $(document).ready(function(){
            $('input').on('change',function(){$(this).val('')})
     }); 

这对我很有效。

其他回答

这里有一个肮脏的hack -

你的元素在这里(添加了disabled属性):

<input type="text" name="test" id="test" disabled="disabled" />

然后在你的网页底部放一些JavaScript:

<script>
    setTimeout(function(){
        document.getElementById('test').removeAttribute("disabled");
        },100);
</script>

我真的不喜欢做隐藏字段,我觉得这样做很快就会让人很困惑。

在您想要停止自动完成的输入字段上,这将起作用。将字段设置为只读并集中,像这样删除该属性

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

这所做的是,你首先要删除只读属性,选择字段,在那个时候,最有可能的是,你将填充与您自己的用户输入和弯腰自动填充接管

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.

我最终通过在输入字段中放入一个不重复的变量来解决这个问题-我使用php time()像这样:

<input type="text" name="town['.time().']" >

这主要是对机器人的干扰。我在服务器端所做的就是在输入名称上做一个foreach循环-问题是如果chrome识别名称属性,它将自动填充。

其他的都对我没用。

我遇到了“现在登录或注册”模式窗口的问题,如果用户已经将他们的凭据保存到浏览器中,这是一个问题。sign in和register字段都被填充了,所以我可以用下面的angular js指令来清除它们:

(function () { "use strict"; var directive = function ($timeout) { return { restrict: "A", link: function (scope, element, attrs) { $timeout(function () { element.val(" "); $timeout(function () { element.val(""); }); }); } }; }; angular.module("app.directives").directive("autofillClear", ["$timeout", directive]); }());

它基本上与之前使用jquery的一些答案相同,但以一种角度的方式完成。