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

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

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

没有“自动完成”属性。

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


当前回答

另一种方法——这也有助于安全性——是每次显示输入框时调用不同的东西:就像captha一样。这样,会话就可以读取一次性的输入,而自动完成就没有什么可做的了。

关于rmeador关于你是否应该干扰浏览器体验的问题,有一点:我们开发联系人管理和CRM系统,当你把别人的数据输入一个表单时,你不希望它不断提示你自己的详细信息。

这可以满足我们的需求,但是我们可以奢侈地告诉用户使用一个不错的浏览器:)

autocomplete='off' 

其他回答

有效的自动补全

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

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.

不理想,但你可以改变id和文本框的名称,每次你渲染它-你必须跟踪它的服务器端,这样你就可以得到数据。

不确定这是否会起作用,这只是一个想法。

另一种方法——这也有助于安全性——是每次显示输入框时调用不同的东西:就像captha一样。这样,会话就可以读取一次性的输入,而自动完成就没有什么可做的了。

关于rmeador关于你是否应该干扰浏览器体验的问题,有一点:我们开发联系人管理和CRM系统,当你把别人的数据输入一个表单时,你不希望它不断提示你自己的详细信息。

这可以满足我们的需求,但是我们可以奢侈地告诉用户使用一个不错的浏览器:)

autocomplete='off' 

如果你使用jQuery,你可以这样做:

$(document).ready(function(){$("input.autocompleteOff").attr("autocomplete","off");});

并在你想要的地方使用autocompleteOff类:

<input type="text" name="fieldName" id="fieldId" class="firstCSSClass otherCSSClass autocompleteOff" />

如果你想要所有的输入都是autocomplete=off,你可以简单地使用:

$(document).ready(function(){$("input").attr("autocomplete","off");});