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 name attribute is used for posting to e.g. a webserver. The id is primarily used for css (and javascript). Suppose you have this setup:
in order to get the value with PHP when posting your form, it will use the name-attribute, like this:
The id is used for styling, as said before, for when you want to use specific css.
Of course, you can use the same denomination for your id and name-attribute. These two will not interfere with each other.
also, name can be used for more items, like when you are using radiobuttons. Name is then used to group your radiobuttons, so you can only select one of those options.
And in this very specific case, I can further say how id is used, because you will probably want a label with your radiobutton. Label has a for-attribute, which uses the id of your input to link this label to your input (when you click the label, the button is checked). Example can be found below
And I guess that's about it.
The
name
attribute on an input is used by its parent HTML<form>
s to include that element as a member of the HTTP form in aPOST
request or the query string in aGET
request.The
id
should be unique as it should be used by JavaScript to select the element in the DOM for manipulation and used in CSS selectors.The name definies what the name of the attribute will be as soon as the form is submitted. So if you want to read this attribute later you will find it under the "name" in the POST or GET Request.
Whereas the id is used to adress a field or element in javascript or css.
If you are using JavaScript/CSS, you must use 'id' of control to apply any CSS/JavaScript stuff on it.
If you use name, CSS won't work for that control. As an example, if you use a JavaScript calendar attached to a textbox, you must use id of text control to assign it the JavaScript calendar.
IDs must be unique
...within page DOM element tree so each control is individually accessible by its
id
on the client side (within browser page) byNames are quite often unique but can be shared
...within page DOM between several controls of the same type (think of radio buttons) so when data gets POSTed to server only a particular value gets sent. So when you have several radio buttons on your page, only the selected one's
value
gets posted back to server even though there are several related radio button controls with the samename
.When names can be duplicated
It may sometimes be beneficial that names are shared between controls of any form input type. But when? You didn't state what your server platform may be, but if you used something like Asp.net MVC you get the benefit of automatic data validation (client and server) and also binding sent data to strong types. That means that those names have to match type property names.
Now suppose you have this scenario:
So your view's model (since it displays a list) is of type
IEnumerable<SomeType>
but your server side only accepts one single item of typeSomeType
.How about name sharing then?
Each item is wrapped within its own
FORM
element and input elements within it have the same names so when data gets to the server (from any element) it gets correctly bound to the string type expected by the controller action.This particular scenario can be seen on my Creative stories mini-site. You won't understand the language, but you can check out those multiple forms and shared names. Never mind that
ID
s are also duplicated (which is a rule violation) but that could be solved. It just doesn't matter in this case.name is used for form submission in DOM (Document Object Model).
ID is used to unique name of html controls in DOM specially for Javascript & CSS