当使用xhtml1-transitional。使用以下HTML收集一个信用卡号

<input type="text" id="cardNumber" name="cardNumber" autocomplete='off'/>

将在W3C验证器上标记一个警告:

没有“自动完成”属性。

是否有一种符合标准的方法来禁用浏览器自动完成表单中的敏感字段?


当前回答

这个解决方案对我很管用:

$('form,input,select,textarea').attr("autocomplete", "nope");

如果你想在这个区域使用自动填充:在元素中添加autocomplete="false" 例:

<input id="search" name="search" type="text" placeholder="Name or Code" autcomplete="false">

其他回答

我建议捕捉所有4种类型的输入:

$('form,input,select,textarea').attr("autocomplete", "off");

参考:

http://www.w3.org/Submission/web-forms2/#the-autocomplete http://dev.w3.org/html5/markup/input.html

下面是一篇来自MDC的好文章,它解释了形成自动补全的问题(和解决方案)。 微软在这里也发布了类似的东西。

老实说,如果这对你的用户很重要,用这种方式“打破”标准似乎是合适的。例如,亚马逊使用“自动完成”属性相当多,它似乎工作得很好。

如果您想完全删除警告,可以使用JavaScript使用someForm将该属性应用到支持它的浏览器(IE和Firefox是重要的浏览器)。setAttribute("autocomplete", "off");someFormElm。setAttribute("autocomplete", "off");

最后,如果你的网站使用的是HTTPS, IE会自动关闭自动完成功能(据我所知,其他一些浏览器也是这样做的)。

更新

由于这个答案仍然得到了相当多的赞,我只是想指出,在HTML5中,你可以在你的表单元素上使用“autocomplete”属性。请参阅W3C上的文档。

Note that there's some confusion about location of the autocomplete attribute. It can be applied either to the whole FORM tag or to individual INPUT tags, and this wasn't really standardized before HTML5 (that explicitly allows both locations). Older docs most notably this Mozilla article only mentions FORM tag. At the same time some security scanners will only look for autocomplete in INPUT tag and complain if it's missing (even if it is in the parent FORM). A more detailed analysis of this mess is posted here: Confusion over AUTOCOMPLETE=OFF attributes in HTML forms.

有效的自动补全

<script type="text/javascript">
    /* <![CDATA[ */
    document.write('<input type="text" id="cardNumber" name="cardNumber" autocom'+'plete="off"/>');
    /* ]]> */ 
</script>

如果W3C提出了一种与(X)HTML4一起工作的方法,我会感到非常惊讶。自动补全功能完全是基于浏览器的,是在过去几年中引入的(远远落后于HTML4标准的编写)。

不过,如果HTML5有这样的功能也不足为奇。

编辑:正如我所想,HTML5确实有这个功能。要将页面定义为HTML5,请使用以下doctype(即:将其作为源代码中的第一个文本)。请注意,并非所有浏览器都支持这个标准,因为它仍处于草案阶段。

<!DOCTYPE html>