The difference between ID and name in the form (input)

Source: Internet
Author: User

The difference between ID and name in the form (input)

But name cannot be substituted for the following purposes:
1. The control name of the form (form), and the submitted data is controlled by the control's name instead of the ID. Because there are many name that correspond to multiple controls at the same time, such as checkbox and radio, the ID must be unique in the full-text file. In addition, the browser sets the request sent to the server according to the name. Therefore, if the ID is used, the server cannot get the data.
2. The name of frame and window for assigning target to another frame or window.

Example: <framesetcols= "200,*" >

<framesrc= "/example/html/toc.html" >

<framesrc= "/example/html/pref.html" name= "View_frame" >

</frameset>

Equivalent to other tags in target= "View_window":

<ul>

<li><a href= "/example/html/pref.html" target= "View_window">Preface</a></li>

<li><a href= "/example/html/chap1.html" target= "View_window">Chapter1</a></li>

</ul>

The following two are common, but it is strongly recommended that you do not use name with ID:
1. Anchor points, usually previously written <a name= "MyName" > but the Name property can only be positioned for a label, and now it is possible to specify with any element ID: <div id= "myID" >.

Example 1:<a href= "001" > Skip to 001</a>

......

<aid= "001" >&nbsp; (for compatibility, a tag cannot be empty) </a>

The value of the href must be the same as the ID, preceded by #

Example 2: Want to display an anchor point content for a page

<ahref= "123.html#001" > Jump to 001</a>

......

<aid= "001" >&nbsp;</a>


The following can only be used with ID:
1. Label is associated with the form control,
<label for= "Myinput" >MyInput</label>
<input id= "Myinput" type= "text" >
The For property specifies the ID of the element associated with the label and is not substituted with name.
2. CSS element selection mechanism, in a #myid way to specify the element to apply the style, cannot be substituted with name.


3. Get the object in the script:

3.1 If using the DOM, use document.getElementById ("Myinput"). Value,

Document.getelementbytagname ("Myinput"). Value

JQ in--$ (". FirstName"). Value

3.2 Submit Form-If you want to use name, you usually get the form containing the control first, such as document.forms["MyForm"], and then refer to name from the form, note that this is calculated will be sent to the server value.

Example:

<scripttype= "Text/javascript" >

Functionformsubmit () {

document.forms["MyForm"]. Submit ();

}

</script>

<body>

<form name= "MyForm"action= "http://www.jb51.net/example/html/form_action.asp" method= "Get" >

Firstname: <input type= "text" name= "fname"/><br/>

Last Name: <input type= "text" name= "lname"/><br/>

<inputtype= "button" onclick= "Formsubmit ()" value= "Send formdata!"/>

</form>

</body>

The difference between name and ID is:
ID to meet the requirements of identification, such as case sensitivity, it is best not to include an underscore (because it is incompatible with CSS). and name basically has no requirements, and can even use numbers.
Add: Name is primarily a property of the form element. Through the JS document. form name. The text box. Value to get the value of the text box, where the form name and text box name refer to name, not form elements such as Div,span, and so on, without the name attribute, and the id attribute is available for any HTML element. When you need to use JS to get non-form element object is to use document.getElementById ("id")

The difference between ID and name in the form (input)

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.