Html 自动完成关闭还是错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30053167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Autocomplete off vs false?
提问by Cross2004
Recently I have come across an issue where I wanted to disable auto-complete in all browsers.
最近我遇到了一个问题,我想在所有浏览器中禁用自动完成。
Chrome has a new feature in settings where you can add a card number. And the requirement was to also disable that.
Chrome 在设置中有一项新功能,您可以在其中添加卡号。并且要求也禁用它。
What worked in all browsers was to do this autocomplete=false
at form level.
在所有浏览器中起作用的是autocomplete=false
在表单级别执行此操作。
But this is not compliant with w3 rules, where they enforce to have autocomplete=off|on
.
但这不符合 w3 规则,他们强制要求拥有autocomplete=off|on
.
Can someone please explain to me why false
works in all browsers?
有人可以向我解释为什么false
在所有浏览器中都有效吗?
even ie8, all firefox, safari etc., but it is not compliant.
甚至ie8,所有firefox,safari等,但它不兼容。
采纳答案by camiblanch
You are right. Setting the autocomplete attribute to "off" does not disable Chrome autofill in more recent versions of Chrome.
你是对的。将自动完成属性设置为“关闭”不会在较新版本的 Chrome 中禁用 Chrome 自动填充。
However, you can set autocomplete to anything besides "on" or "off" ("false", "true", "nofill") and it will disable Chrome autofill.
但是,您可以将自动完成设置为除“on”或“off”(“false”、“true”、“nofill”)之外的任何内容,它会禁用 Chrome 自动填充。
This behavior is probably because the autocomplete attribute expects either an "on" or "off" value and doesn't do anything if you give it something else. So if you give it something other than those values, autofill falls apart/doesn't do anything.
这种行为可能是因为 autocomplete 属性需要一个“on”或“off”值,如果你给它其他东西,它不会做任何事情。因此,如果您给它提供除这些值以外的其他值,则自动填充会分崩离析/不做任何事情。
With the current version of Chrome it has been found that setting the autocomplete attribute to "off" actually works now.
在当前版本的 Chrome 中,我们发现将自动完成属性设置为“关闭”现在实际上可以工作。
Also, I have found that this only works if you set the autocomplete attribute in each <input>
tag of the form.
此外,我发现这仅在您<input>
在表单的每个标签中设置自动完成属性时才有效。
There has been a response to this ambiguity in the Chromium bug listings here.
在此处的 Chromium 错误列表中已经对这种含糊不清的情况作出了回应。
Disclaimer: This was found to be true in Chrome version 47.0.2526.106 (64-bit)
免责声明:在 Chrome 版本 47.0.2526.106(64 位)中发现这是真的
回答by blairzotron
Use autocomplete="my-field-name"instead of autocomplete="off". Be careful what you call it, since some values are still recognized like autocomplete="country". I also found that using the placeholder attribute helped in some tricky scenarios.
使用autocomplete="my-field-name"而不是autocomplete="off"。小心你怎么称呼它,因为某些值仍然可以识别,例如 autocomplete="country"。我还发现使用占位符属性有助于解决一些棘手的情况。
Example:<input type="text" name="field1" autocomplete="my-field-name1" placeholder="Enter your name">
例子:<input type="text" name="field1" autocomplete="my-field-name1" placeholder="Enter your name">
Chrome recently stopped using autocomplete="off"because they thought it was overused by developers who didn't put much thought into whether or not the form should autocomplete. Thus they took out the old method and made us use a new one to ensure we really don't want it to autocomplete.
Chrome 最近停止使用autocomplete="off" ,因为他们认为它被开发人员过度使用,他们没有考虑表单是否应该自动完成。因此,他们取消了旧方法并让我们使用新方法以确保我们真的不希望它自动完成。
回答by Adheep Mohamed Abdul Kader
After Chrome version 72.XX:
Chrome 72.XX 版之后:
Chrome ignores autocomplete="off"
autocomplete="no-fill"
or autocomplete="randomText"
both on field and form level.
Chrome在字段和表单级别忽略autocomplete="off"
autocomplete="no-fill"
或autocomplete="randomText"
同时忽略。
The only option I found is to follow a work-around by tricking Chrome to populate the autofill on a dummy Textbox and password and then hide them from the user view.
我发现的唯一选择是通过欺骗 Chrome 在虚拟文本框和密码上填充自动填充,然后将它们从用户视图中隐藏来遵循解决方法。
Remember the old method with style="display: hidden"
or style="visibility: hidden"
is also ignored.
记住带有style="display: hidden"
or的旧方法style="visibility: hidden"
也被忽略。
FIX:
使固定:
So create a DIV
with height: 0px;overflow:hidden
which will still render the HTML elements but hide them from User's view.
因此,创建一个仍将呈现 HTML 元素但将它们从用户视图中隐藏的DIV
with height: 0px;overflow:hidden
。
Sample Code:
示例代码:
<div style="overflow: hidden; height: 0px;background: transparent;" data-description="dummyPanel for Chrome auto-fill issue">
<input type="text" style="height:0;background: transparent; color: transparent;border: none;" data-description="dummyUsername"></input>
<input type="password" style="height:0;background: transparent; color: transparent;border: none;" data-description="dummyPassword"></input>
</div>
Just add the above div within the HTML Form and it should work!
只需在 HTML 表单中添加上面的 div 就可以了!
回答by wintel-warrior
If anyones reading this and is having difficulty disabling autocomplete on username and password fields for new users, I found setting autocomplete="new-password" works in Chrome 77. It also prevented the username field from auto completing.
如果有人阅读本文并且在为新用户禁用用户名和密码字段的自动完成功能时遇到困难,我发现设置 autocomplete="new-password" 在 Chrome 77 中有效。它还阻止了用户名字段自动完成。
Ref: https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofill
参考:https: //html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofill
回答by Kishor Pawar
$("#selector").attr("autocomplete", "randomString");
This has worked reliably everytime for me.
这对我来说每次都很可靠。
Note : I have invoked this LOC on modal show event.
注意:我在模态显示事件上调用了这个 LOC。
回答by Rajendran Nadar
Auto complete value other that off and false works.
除了 off 和 false 工作之外的自动完成值。
autoComplete="nope"
autoComplete="foo"
autoComplete="boo"
autoComplete="anythingGoesHere"
autoComplete="nope"
autoComplete="foo"
autoComplete="boo"
autoComplete="anythingGoesHere"
Tested on chrome 76 and react 16.9.0
在 chrome 76 上测试并反应 16.9.0
回答by Rohit Parte
autocomplete="none"perfectly works for angularjs and angular 7
autocomplete="none"完美适用于 angularjs 和 angular 7
回答by Matas Vaitkevicius
As an addition to @camiblanch answer
作为@camiblanch 答案的补充
Adding autocomplete="off"
is not gonna cut it.
Change input type attribute to type="search"
.
Google doesn't apply auto-fill to inputs with a type of search.
添加autocomplete="off"
不会削减它。
将输入类型属性更改为type="search"
.
Google 不会对具有搜索类型的输入应用自动填充。