Is there a W3C valid way to disable autocomplete in a HTML form?

后端 未结 18 1127
心在旅途
心在旅途 2020-11-22 05:58

When using the xhtml1-transitional.dtd doctype, collecting a credit card number with the following HTML



        
相关标签:
18条回答
  • 2020-11-22 06:30

    Not ideal, but you could change the id and name of the textbox each time you render it - you'd have to track it server side too so you could get the data out.

    Not sure if this will work or not, was just a thought.

    0 讨论(0)
  • 2020-11-22 06:30

    This solution works with me:

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

    if you want use autofill in this region: add autocomplete="false" in element ex:

    <input id="search" name="search" type="text" placeholder="Name or Code" autcomplete="false">
    
    0 讨论(0)
  • 2020-11-22 06:32

    I would be very surprised if W3C would have proposed a way that would work with (X)HTML4. The autocomplete feature is entirely browser-based, and was introduced during the last years (well after the HTML4 standard was written).

    Wouldn't be surprised if HTML5 would have one, though.

    Edit: As I thought, HTML5 does have that feature. To define your page as HTML5, use the following doctype (i.e: put this as the very first text in your source code). Note that not all browsers support this standard, as it's still in draft-form.

    <!DOCTYPE html>
    
    0 讨论(0)
  • 2020-11-22 06:34

    HTML 4: No

    HTML 5: Yes

    The autocomplete attribute is an enumerated attribute. The attribute has two states. The on keyword maps to the on state, and the off keyword maps to the off state. The attribute may also be omitted. The missing value default is the on state. The off state indicates that by default, form controls in the form will have their autofill field name set to off; the on state indicates that by default, form controls in the form will have their autofill field name set to "on".

    Reference: W3

    0 讨论(0)
  • 2020-11-22 06:35

    Using a random 'name' attribute works for me.

    I reset the name attribute when sending the form so you can still access it by name when the form is sent. (using the id attribute to store the name)

    0 讨论(0)
  • 2020-11-22 06:36
    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");
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题