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

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

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

没有“自动完成”属性。

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


当前回答

我在2020年就做到了!

我基本上创建了一个css类,对我的输入应用-webkit-text-security。

这里有一个最近讨论的链接: https://stackoverflow.com/a/64471795/8754782

其他回答

我在2020年就做到了!

我基本上创建了一个css类,对我的输入应用-webkit-text-security。

这里有一个最近讨论的链接: https://stackoverflow.com/a/64471795/8754782

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.

if (document.getElementsByTagName) {
    var inputElements = document.getElementsByTagName("input");
    for (i=0; inputElements[i]; i++) {
        if (inputElements[i].className && (inputElements[i].className.indexOf("disableAutoComplete") != -1)) {
            inputElements[i].setAttribute("autocomplete","off");
        }
    }
}

我建议捕捉所有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

有效的自动补全

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