Detailed explanation of the difference between name and id in the form element input box

迷茫
Release: 2017-03-25 16:22:46
Original
2076 people have browsed it

1. It can be said that almost everyone who has done web development has asked, what is the difference between the ID and Name of an element? Why do we need Name when we have ID?! And we can also get the most classical answer: ID is like a person's ID number, and Name is like his name. ID is obviously unique, and Name is repeatable.


Last week I also encountered the problem of ID and Name. I entered an input on the page. type="hidden", only one ID='SliceInfo' is written. After assigning the value, submit, and use Request.Params["SliceInfo"] in the background, but the value cannot be obtained. Later, I suddenly realized that it should be marked with Name, so I added Name='SliceInfo' to the input, and everything was ok.

The answer to ID and Name in the first paragraph is too general. Of course, that explanation is completely correct for ID, which is the Identity of the HTML element on the client side. Name is actually much more complicated, because Name has many uses, so it cannot be completely replaced by ID, thus canceling it. The specific uses are:

Use 1: As a server-side indicator of HTML elements that can interact with the server, such as input, select, textarea, and button. We can get the value submitted by the element through Request.Params based on its Name on the server side.
Use 2: HTML element Input type='radio' grouping, we know that the radio button control is in the same grouping class, the check operation is mutex, only one radio can be selected at the same time, this grouping is based on the same Name attribute realized.
Use 3: To establish the anchor point in the page, we knowlinkis to obtain a page hyperlink. If you do not use the href attribute, use Name instead, such as:, we get a page anchor.
Use 4: Identity as an object, such as Applet, Object, Embed and other elements. For example, in an Applet object instance, we will use its Name to refer to the object.
Purpose 5: When associating between IMG elements and MAP elements, if you want to define the hotspot area of IMG, you need to use its attribute usemap, so usemap="#name" (Name of the associated MAP element).
Use 6: Attributes of some specific elements, such as attribute, meta and param.

For example, defining parameters for Object

Copy after login

or

Copy after login

in Meta. Obviously these uses cannot be simply replaced by ID, so the HTML element The difference between ID and Name is not the difference between ID number and name. They have different functions.

Form elements (form input textarea select) and frame elements (iframe frame) use name
These elements are related to the submission of the form (the frame element acts on the target of the form). When receiving the form The page only receives elements with names. Elements assigned IDs cannot receive values through the form. You can verify it yourself.
There is an exception: A can be assigned name as an anchor point or an ID.

2.id must meet the requirements of identification, such as case sensitivity, and it is best not to include underscores (because it is not compatible with CSS). There are basically no requirements for name, and you can even use numbers. ID is like a person's ID number, and Name is like his name. ID is obviously unique, but Name can be repeated.

tag specifies an input field into which the user can enter data.
According to different type attributes, input fields have various forms. Input fields can be text fields, checkboxes, password fields, radio buttons, buttons, etc.
id: as the unique identifier of the tag.
name: The server-side identifier of an HTML element that can interact with the server.

3.ID is a CSS style defined with

#You can also use JS to control getElementById (here is the ID) to get the
name a lot, which is to give the current tag or element Specify the name, or you can use JS to control the value. When obtaining the form after submission, you need to obtain the name.

4.id is generally used for reference in css and js, and name is used for form submission. Only tag elements with the name attribute added will be submitted to the server.

The above is the detailed content of Detailed explanation of the difference between name and id in the form element input box. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!