When using the HTML <input>
tag, what is the difference between the use of the name
and id
attributes especially that I found that they are sometimes named the same?
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a way to play audio on a mobile browser w
- HTML form is not sending $_POST values
- implementing html5 drag and drop photos with knock
-
Why does the box-shadow property not apply to a
The id is used to uniquely identify an element in JavaScript or CSS.
The name is used in form submission. When you submit a form only the fields with a name will be submitted.
Adding some actual references to W3 docs that authoritatively explain the role of the 'name' attribute on form elements. (For what it's worth, I arrived here while exploring exactly how Stripe.js works to implement safe interaction with payment gateway Stripe. In particular, what causes a form input element to get submitted back to the server, or prevents it from being submitted?)
The following W3 docs are relevent:
HTML 4: https://www.w3.org/TR/html401/interact/forms.html#control-name Section 17.2 Controls
HTML 5: https://www.w3.org/TR/html5/forms.html#form-submission-0 and https://www.w3.org/TR/html5/forms.html#constructing-the-form-data-set Section 4.10.22.4 Constructing the form data set.
As explained therein, an input element will be submitted by the browser if and only if it has a valid 'name' attribute.
As others have noted, the 'id' attribute uniquely identifies DOM elements, but is not involved in normal form submission. (Though 'id' or other attributes can of course be used by javascript to obtain form values, which javascript could then use for AJAX submissions and so on.)
One oddity regarding previous answers/commenters concern about id's values and name's values being in the same namespace. So far as I can tell from the specs, this applied to some deprecated uses of the name attribute (not on form elements). For example https://www.w3.org/TR/html5/obsolete.html:
Clearly in this special case there's some overlap between id and name values for 'a' tags. But this seems to be a peculiarity of processing for fragment ids, not due to general sharing of namespace of ids and names.
I hope you can find the following brief example helpful:
In the code, note that both 'name' attributes are the same to define optionality between 'male' or 'female', but the 'id's are not equals to differentiate them.
name
identifies form fields* ; so they can be shared by controls that stand to represent multiple possibles values for such a field (radio buttons, checkboxes). They will be submitted as keys for form values.id
identifies DOM elements ; so they can be targeted by CSS or Javascript.* names also used to identify local anchors, but this is deprecated and 'id' is a preferred way to do so nowadays.
name
is the name that is used when the value is passed (in the url or in the posted data).id
is used to uniquely identify the element for CSS styling and JavaScript.The
id
can be used as an anchor too. In the old days,<a name
was used for that, but you should use theid
for anchors too.name
is only to post form data.In HTML4.01:
Name Attribute
<a>
,<form>
,<iframe>
,<img>
,<map>
,<input>
,<select>
,<textarea>
getElementsByName()
id
attributename
attribute are submitted to the serverId Attribute
<base>
,<html>
,<head>
,<meta>
,<param>
,<script>
,<style>
,<title>
#
signgetElementById()
, and jQuery by$(#<id>)
_
), dashes (-
), colons (:
), or periods (.
)In (X)HTML5, everything is the same except:
Name Attribute
<form>
anymoreId Attribute
This question was written when HTML4.01 was the norm, and many browsers and features were different from today.