当使用xhtml1-transitional。使用以下HTML收集一个信用卡号
<input type="text" id="cardNumber" name="cardNumber" autocomplete='off'/>
将在W3C验证器上标记一个警告:
没有“自动完成”属性。
是否有一种符合标准的方法来禁用浏览器自动完成表单中的敏感字段?
当使用xhtml1-transitional。使用以下HTML收集一个信用卡号
<input type="text" id="cardNumber" name="cardNumber" autocomplete='off'/>
将在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.
其他回答
我建议捕捉所有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
另一种方法——这也有助于安全性——是每次显示输入框时调用不同的东西:就像captha一样。这样,会话就可以读取一次性的输入,而自动完成就没有什么可做的了。
关于rmeador关于你是否应该干扰浏览器体验的问题,有一点:我们开发联系人管理和CRM系统,当你把别人的数据输入一个表单时,你不希望它不断提示你自己的详细信息。
这可以满足我们的需求,但是我们可以奢侈地告诉用户使用一个不错的浏览器:)
autocomplete='off'
如果W3C提出了一种与(X)HTML4一起工作的方法,我会感到非常惊讶。自动补全功能完全是基于浏览器的,是在过去几年中引入的(远远落后于HTML4标准的编写)。
不过,如果HTML5有这样的功能也不足为奇。
编辑:正如我所想,HTML5确实有这个功能。要将页面定义为HTML5,请使用以下doctype(即:将其作为源代码中的第一个文本)。请注意,并非所有浏览器都支持这个标准,因为它仍处于草案阶段。
<!DOCTYPE html>
不,但是浏览器自动完成通常是由字段具有与之前填写的字段相同的name属性触发的。如果您可以使用一种聪明的方法来拥有一个随机的字段名称,那么自动补全将无法提取该字段之前输入的任何值。
如果你给一个输入字段一个像“email_<?”= randomNumber() ?>”,然后让接收此数据的脚本通过POST或GET变量循环查找与模式“email_[some number]”匹配的内容,您可以完成此操作,并且这将(实际上)保证成功,无论使用哪种浏览器。
如果你使用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");});