It can be said that almost everyone who has done Web development has asked, What is the difference between the element ID and Name? Why do I need a Name if I have an ID ?! We can also get the most classical answer: ID is like a person's ID card number, and Name is like his Name. ID is obviously unique, and Name can be repeated.
Last week, I also encountered the ID and Name problems. I entered an input type = "hidden" on the page and wrote only one ID = 'sliceinfo'. After the assignment, I submit it, use Request. params ["SliceInfo"] does not return a value. Later, I suddenly realized that the Name should be used for marking, so I added a Name = 'sliceinfo' to the input, and everything was okay.
The answer to ID and Name in the first section is too general. Of course, that explanation is completely correct for ID. It is the Identity of the HTML element on the Client side. Name is actually much more complex, because Name has many purposes, so it cannot be completely replaced by ID, so it is canceled. Specific uses include:
Purpose 1: ID of the server as an HTML element that can interact with the server, such as input, select, textarea, And button. On the server side, we can use Request. Params to obtain the value submitted by the element based on its Name.
Purpose 2: HTML element Input type = 'radio' group. We know that the radio button control is in the same group class. The check operation is mutex and only one radio can be selected at a time, this group is implemented based on the same Name attribute.
Purpose 3: Create an anchor in the page. We know that <a href = "URL"> link </a> is used to obtain a page hyperlink. If you do not use the href attribute, use Name instead, for example, <a name = "PageBottom"> </a>.
Purpose 4: act as the Identity of an Object, such as Applet, Object, and Embed. For example, in the Applet object instance, we will use its Name to reference this object.
Purpose 5: When the IMG and MAP elements are associated, if you want to define the IMG hotspot area, you need to use its attribute usemap, make usemap = "# name" (Name of the associated MAP element ).
Purpose 6: Attributes of certain elements, such as attribute and param. For example, define the parameter <param name = "appletParameter" VALUE = "value"> for the Object.
Obviously, these functions are not simply replaced by IDS, so the ID and Name of the HTML element are not the same as the ID card number and Name, they are actually different things.
Of course, the Name attribute of the HTML element can also play a role of ID in the page, because in the DHTML Object Tree, we can use document. getElementsByName is used to obtain an array of objects containing all the specified Name elements on the page.
By the way, what if the IDs of n (n> 1) HTML elements on the page are the same? How can we reference them in DHTML objects? If we use the ASPX page, this situation is not easy, because the aspnet process does not allow non-unique IDs when processing the aspx page, this is because the page will be thrown an exception and cannot be a normal render. If we want to repeat the ID on a dynamic page, how does IE do it? At this time, we can continue to use document. getElementById to get the object, except that we can only get the first object that appears in the HTML Render of the objects with duplicate IDs. At this time, duplicate IDs are automatically converted into an array when being referenced. Elements with duplicate IDs exist in the array in order of Render.