The difference between name and id in the input box, And nameid in the input box

Source: Internet
Author: User

The difference between name and id in the input box, And nameid in the input box

1.

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, meta, And param. For example, define <param name = "appletParameter" VALUE = "value"> for an Object or <Meta NAME = "Author" CONTENT = "Dave Raggett"> in META.

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.

Form input textarea select and iframe frame use name

These elements are related to the submission of forms (the Framework element acts on the target of the form). Only the element with name is received on the receiving page of the form, and the element with ID cannot receive the value through the form, you can verify it yourself.

One exception is that A can assign A name as the anchor or ID.

2.

The id must comply with the logo requirements, such as case sensitivity. It is best not to include underscores (because CSS is not compatible ). There are basically no requirements for name, and even numbers can be used. ID is like a person's ID card number, and Name is like his Name. ID is obviously unique, and Name can be repeated.

<Input> the tag specifies the input fields for data input.

Based on different type attributes, input fields have multiple forms. The input fields can be text fields, check boxes, password fields, single-choice buttons, and buttons.
Id: the unique identifier of a tag.

Name: indicates the server side of the HTML element that can interact with the server.

3.

ID is a CSS style defined #.

You can also use JS to control getElementById (ID here) to obtain

There are a lot of names, that is, to specify a name for the current tag or element, you can also use JS to control the value, the name needs to be obtained after the form is submitted.

4.

Id is generally used for reference in css and js, and name is used for form submission. Only the label elements with the name attribute are submitted to the server.

The above is the difference between name and id in the input box introduced by the editor. I hope it will help you. If you have any questions, please leave a message and the editor will reply to you in time. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.