当使用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验证器上标记一个警告:
没有“自动完成”属性。
是否有一种符合标准的方法来禁用浏览器自动完成表单中的敏感字段?
当前回答
使用随机的“name”属性对我来说很有用。
我在发送表单时重置了name属性,以便在发送表单时仍然可以通过名称访问它。(使用id属性存储名称)
其他回答
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.
不,但是浏览器自动完成通常是由字段具有与之前填写的字段相同的name属性触发的。如果您可以使用一种聪明的方法来拥有一个随机的字段名称,那么自动补全将无法提取该字段之前输入的任何值。
如果你给一个输入字段一个像“email_<?”= randomNumber() ?>”,然后让接收此数据的脚本通过POST或GET变量循环查找与模式“email_[some number]”匹配的内容,您可以完成此操作,并且这将(实际上)保证成功,无论使用哪种浏览器。
用JavaScript设置怎么样?
var e = document.getElementById('cardNumber');
e.autocomplete = 'off'; // Maybe should be false
这并不完美,但您的HTML将是有效的。
我认为有一个更简单的方法。 用一个随机的名字(通过javascript)创建一个隐藏的输入,并设置用户名。重复输入密码。这样,您的后端脚本就可以确切地知道适当的字段名称是什么,同时保持自动补全在黑暗中。
我可能错了,但这只是个想法。
我建议捕捉所有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