是否有 W3C 有效的方法来禁用 HTML 表单中的自动完成功能?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/582244/
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
Is there a W3C valid way to disable autocomplete in a HTML form?
提问by matt b
When using the xhtml1-transitional.dtd
doctype, collecting a credit card number with the following HTML
使用xhtml1-transitional.dtd
doctype 时,使用以下 HTML 收集信用卡号
<input type="text" id="cardNumber" name="cardNumber" autocomplete='off'/>
will flag a warning on the W3C validator:
将在 W3C 验证器上标记警告:
there is no attribute "autocomplete".
没有属性“自动完成”。
Is there a W3C / standards way to disable browser auto-complete on sensitive fields in a form?
是否有 W3C / 标准方法来禁用表单中敏感字段的浏览器自动完成功能?
采纳答案by Nick Presta
Hereis a good article from the MDC which explains the problems (and solutions) to form autocompletion. Microsoft has published something similar here, as well.
这是来自 MDC 的一篇很好的文章,它解释了形成自动完成的问题(和解决方案)。微软也在这里发布了类似的东西。
To be honest, if this is something important to your users, 'breaking' standards in this way seems appropriate. For example, Amazon uses the 'autocomplete' attribute quite a bit, and it seems to work well.
老实说,如果这对您的用户很重要,那么以这种方式“打破”标准似乎是合适的。例如,亚马逊相当多地使用了“自动完成”属性,它似乎运行良好。
If you want to remove the warning entirely, you can use JavaScript to apply the attribute to browsers that support it (IE and Firefox are the important browsers) using someForm.setAttribute( "autocomplete", "off" ); someFormElm.setAttribute( "autocomplete", "off" );
如果要完全删除警告,可以使用 JavaScript 将该属性应用到支持它的浏览器(IE 和 Firefox 是重要的浏览器)使用 someForm.setAttribute( "autocomplete", "off" ); someFormElm.setAttribute( "autocomplete", "off" );
Finally, if your site is using HTTPS, IE automatically turns off autocompletion (as do some other browsers, as far as I know).
最后,如果您的站点使用 HTTPS,IE 会自动关闭自动完成功能(据我所知,其他一些浏览器也是如此)。
Update
更新
As this answer still gets quite a few upvotes, I just wanted to point out that in HTML5, you can use the 'autocomplete' attribute on your form element. See the documentationon W3C for it.
由于这个答案仍然获得了相当多的支持,我只想指出,在 HTML5 中,您可以在表单元素上使用“自动完成”属性。请参阅有关 W3C的文档。
回答by Henrik Paul
I would be very surprised if W3C would have proposed a way that would work with (X)HTML4. The autocomplete feature is entirely browser-based, and was introduced during the last years (well after the HTML4 standard was written).
如果 W3C 会提出一种与 (X)HTML4 一起工作的方法,我会感到非常惊讶。自动完成功能完全基于浏览器,并且是在过去几年中引入的(在 HTML4 标准编写之后)。
Wouldn't be surprised if HTML5 would have one, though.
不过,如果 HTML5 有一个,也不会感到惊讶。
Edit:As I thought, HTML5 doeshave thatfeature. To define your page as HTML5, use the following doctype (i.e: put this as the very first text in your source code). Note that not all browsers support this standard, as it's still in draft-form.
编辑:正如我所想,HTML5确实具有该功能。要将您的页面定义为 HTML5,请使用以下文档类型(即:将其作为源代码中的第一个文本)。请注意,并非所有浏览器都支持此标准,因为它仍处于草案形式。
<!DOCTYPE html>
回答by RuudKok
HTML 4: No
HTML 4:否
HTML 5: Yes
HTML 5:是的
The autocomplete attribute is an enumerated attribute. The attribute has two states. The onkeyword maps to the on state, and the offkeyword maps to the off state. The attribute may also be omitted. The missing value default is the onstate. The offstate indicates that by default, form controls in the form will have their autofill field name set to off; the on state indicates that by default, form controls in the form will have their
autofill
field name set to "on".
autocomplete 属性是一个枚举属性。该属性有两种状态。在上关键字映射到导通状态,并且关闭关键字映射到关断状态。也可以省略该属性。缺失值默认为开启状态。该关闭状态表示在默认情况下,在形式表单控件都会有自己的自动填充字段名称设置为关闭; on 状态表示默认情况下,表单中的表单控件会将其
autofill
字段名称设置为“on”。
Reference: W3
参考:W3
回答by Phantom Watson
No, but browser auto-complete is often triggered by the field having the same name
attribute as fields that were previously filled out. If you could rig up a clever way to have a randomized field name, autocomplete wouldn't be able to pull any previously entered values for the field.
否,但浏览器自动完成通常由与name
先前填写的字段具有相同属性的字段触发。如果您可以巧妙地设置随机字段名称,则自动完成将无法为该字段提取任何先前输入的值。
If you were to give an input field a name like "email_<?= randomNumber() ?>
", and then have the script that receives this data loop through the POST or GET variables looking for something matching the pattern "email_[some number]
", you could pull this off, and this would have (practically) guaranteed success, regardless of browser.
如果你给一个输入字段,如“名称email_<?= randomNumber() ?>
”,再有通过POST接收到该信息循环脚本或GET寻找的东西相匹配的模式变量“ email_[some number]
”,你可以把这事办成,而这将有(实际上)保证成功,无论浏览器如何。
回答by David Waters
No, a good article is here in Mozila Wiki.
不,Mozila Wiki 中有一篇好文章。
I would continue to use the invalid attribute
. I think this is where pragmatism should win over validating.
我会继续使用无效的attribute
. 我认为这是实用主义应该战胜验证的地方。
回答by Greg
How about setting it with JavaScript?
用 JavaScript 设置怎么样?
var e = document.getElementById('cardNumber');
e.autocomplete = 'off'; // Maybe should be false
It's not perfect, but your HTML will be valid.
它并不完美,但您的 HTML 将是有效的。
回答by Malartre
I suggest catching all 4 types of input:
我建议捕获所有 4 种类型的输入:
$('form,input,select,textarea').attr("autocomplete", "off");
Reference:
参考:
回答by Totoche
If you use jQuery, you can do something like that :
如果您使用 jQuery,则可以执行以下操作:
$(document).ready(function(){$("input.autocompleteOff").attr("autocomplete","off");});
and use the autocompleteOff class where you want :
并在需要的地方使用 autocompleteOff 类:
<input type="text" name="fieldName" id="fieldId" class="firstCSSClass otherCSSClass autocompleteOff" />
If you want ALL your input to be autocomplete=off
, you can simply use that :
如果您希望所有输入都是autocomplete=off
,您可以简单地使用它:
$(document).ready(function(){$("input").attr("autocomplete","off");});
回答by Enigma Plus
Another way - which will also help with security is to call the input box something different every time you display it: just like a captha. That way, the session can read the one-time only input and Auto-Complete has nothing to go on.
另一种也有助于安全的方法是在每次显示输入框时调用不同的内容:就像 captha 一样。这样,会话就可以读取一次性输入,而自动完成就无事可做。
Just a point regarding rmeador's question of whether you should be interfering with the browser experience: We develop Contact Management & CRM systems, and when you are typing other people's data into a form you don't want it constantly suggesting your own details.
关于 rmeador 是否应该干扰浏览器体验的问题的一点:我们开发了联系人管理和 CRM 系统,当您将其他人的数据输入表单时,您不希望它不断提示您自己的详细信息。
This works for our needs, but then we have the luxury of telling users to get a decent browser:)
这可以满足我们的需求,但是我们可以告诉用户获得一个像样的浏览器:)
autocomplete='off'
回答by Emilio Gort
autocomplete="off"
this should fix the issue for all modern browsers.
autocomplete="off"
这应该可以解决所有现代浏览器的问题。
<form name="form1" id="form1" method="post" autocomplete="off"
action="http://www.example.com/form.cgi">
[...]
</form>
In current versions of Gecko browsers, the autocomplete attribute works perfectly. For earlier versions, going back to Netscape 6.2, it worked with the exception for forms with "Address" and "Name"
在当前版本的 Gecko 浏览器中,自动完成属性工作得很好。对于较早的版本,回到 Netscape 6.2,它可以处理带有“地址”和“名称”的表单的异常
Update
更新
In some cases, the browser will keep suggesting autocompletion values even if the autocomplete attribute is set to off. This unexpected behavior can be quite puzzling for developers. The trick to really forcing the no-autocompletion is to assign a random string to the attribute, for example:
在某些情况下,即使自动完成属性设置为关闭,浏览器也会不断建议自动完成值。对于开发人员来说,这种意外行为可能令人费解。真正强制非自动完成的技巧是为属性分配一个随机字符串,例如:
autocomplete="nope"
Since this random value is not a valid one
, the browser will give up.
由于这个随机值不是 a valid one
,浏览器将放弃。