Difference between id and name attributes in HTML

前端 未结 19 2772
感动是毒
感动是毒 2020-11-22 05:17

What is the difference between the id and name attributes? They both seem to serve the same purpose of providing an identifier.

I would lik

相关标签:
19条回答
  • 2020-11-22 05:39
    <form action="demo_form.asp">
    <label for="male">Male</label>
    <input type="radio" name="sex" id="male" value="male"><br>
    <label for="female">Female</label>
    <input type="radio" name="sex" id="female" value="female"><br>
    <input type="submit" value="Submit">
    </form>
    
    0 讨论(0)
  • 2020-11-22 05:39

    Below is an interesting use of the id attribute. It is used within the tag and used to identify the form for elements outside of the boundaries so that they will be included with the other fields within the form.

     <form action="action_page.php" id="form1">
     First name: <input type="text" name="fname"><br>
     <input type="submit" value="Submit">
     </form>
    
     <p>The "Last name" field below is outside the form element, but still part of the form.</p>
     Last name: <input type="text" name="lname" form="form1">
    
    0 讨论(0)
  • 2020-11-22 05:41

    Id : 1) It is used to identify the HTML element through the Document Object Model (via Javascript or styled with CSS). 2) Id is expected to be unique within the page.

    Name corresponds to the form element and identifies what is posted back to the server. Example :

    <form action="action_page.php" id="Myform">
     First name: <input type="text" name="FirstName"><br>
     <input type="submit" value="Submit">
     </form>
    
     <p>The "Last name" field below is outside the form element, but still part of the form.</p>
     Last name: <input type="text" name="LastName" form="Myform">
    
    0 讨论(0)
  • 2020-11-22 05:42

    name Vs id

    name

    • Name of the element. For example used by the server to identify the fields in form submits.
    • Supporting elements are <button>, <form>, <fieldset>, <iframe>, <input>, <keygen>, <object>, <output>, <select>, <textarea>, <map>, <meta>, <param>
    • Name does not have to be unique.

    id

    • Often used with CSS to style a specific element. The value of this attribute must be unique.
    • Id is Global attributes, they can be used on all elements, though the attributes may have no effect on some elements.
    • Must be unique in the whole document.
    • This attribute's value must not contain white spaces, in contrast to the class attribute, which allows space-separated values.
    • Using characters except ASCII letters and digits, '_', '-' and '.' may cause compatibility problems, as they weren't allowed in HTML 4. Though this restriction has been lifted in HTML 5, an ID should start with a letter for compatibility.
    0 讨论(0)
  • 2020-11-22 05:44

    This link has answers to the same basic question, but basically, id is used for scripting identification and name is for server-side.

    http://www.velocityreviews.com/forums/t115115-id-vs-name-attribute-for-html-controls.html

    0 讨论(0)
  • 2020-11-22 05:46

    The ID of a form input element has nothing to do with the data contained within the element. IDs are for hooking the element with JavaScript and CSS. The name attribute, however, is used in the HTTP request sent by your browser to the server as a variable name associated with the data contained in the value attribute.

    For instance:

    <form>
        <input type="text" name="user" value="bob">
        <input type="password" name="password" value="abcd1234">
    </form>
    

    When the form is submitted, the form data will be included in the HTTP header like this:

    If you add an ID attribute, it will not change anything in the HTTP header. It will just make it easier to hook it with CSS and JavaScript.

    0 讨论(0)
提交回复
热议问题