Observe the differences between ID and name in HTML, and raise your hand automatically when you are fooled by Microsoft.

Source: Internet
Author: User

A long time ago, I remember in ASP. net, as long as the name attribute is added to the server control, there will be a warning, as shown in, over time, that for HTML, the name attribute is redundant and can be removed, until ASP. the emergence of net MVC encourages fine-grained control of HTML display, only to find that it has been cheated by Microsoft for several years!

 

The following is a simple HTML form. Several Single-choice and multi-choice boxes also have a text box. These HTML tags only have the ID attribute and do not have the name attribute.

 

 
 <% Using(Html. beginform ())
 
{%>
 
<Input ID= "Checkbox1" Type= "Checkbox" />
<Input ID= "Checkbox2" Type= "Checkbox" />
 
<Input ID= "Checkbox3" Type= "Checkbox" />
 
<Input ID= "Checkbox4" Type= "Checkbox" />
<Input ID= "Checkbox5" Type= "Checkbox" />
 
<Input ID= "Radio1" Type= "Radio" />
 
<Input ID= "Radio2" Type= "Radio" />
<Input ID= "Radio3"Type= "Radio" />
 
<Input ID= "Text1" Type= "Text" />
 
<Input ID= "Submit1" Type= "Submit" Value= "Submit" />
 
<%}%>

I was shocked when I observed the selection and submitted the form! Neither is the ID attribute.

Submit Form

 

Get

 

Added the name attribute for the tag. For more information, see

 

 <% Using(Html. beginform ())
 
{%>
 
<Input ID= "Checkbox1" Name= "Checkbox1"Type= "Checkbox" />
 
<Input ID= "Checkbox2" Name= "Checkbox2" Type= "Checkbox" />
<Input ID= "Checkbox3" Name= "Checkbox3"Type= "Checkbox" />
 
<Input ID= "Checkbox4" Name= "Checkbox4" Type= "Checkbox" />
 
<Input ID= "Checkbox5" Name= "Checkbox5"Type= "Checkbox" />
<Input ID= "Radio1" Name= "Radio1"Type= "Radio" />
 
<Input ID= "Radio2" Name= "Radio2" Type= "Radio" />
 
<Input ID= "Radio3" Name= "Radio3"Type= "Radio" />
<Input ID= "Text1" Name= "Text1" Type= "Text" />
 
<Input ID= "Submit1" Type= "Submit" Value= "Submit" />
 
<%}%>

 

Submit the form and find data. The original name attribute is very useful;

For multiple and single choice, the default value is "on"

The default value of the data received by the server is "on"

 

For multiple choice and single choice, I changed the name attribute to the same, group, and observe

 

 
 <% Using(Html. beginform ())
 
{%>
 
<Input ID= "Checkbox1" Name= "Checkbox"Type= "Checkbox"/>
 
<Input ID= "Checkbox2" Name= "Checkbox" Type= "Checkbox"/>
<Input ID= "Checkbox3" Name= "Checkbox"Type= "Checkbox"/>
 
<Input ID= "Checkbox4" Name= "Checkbox" Type= "Checkbox"/>
 
<Input ID= "Checkbox5" Name= "Checkbox"Type= "Checkbox"/>
<Input ID= "Radio1" Name= "Radio"Type= "Radio"/>
 
<Input ID= "Radio2" Name= "Radio" Type= "Radio"/>
 
<Input ID= "Radio3" Name= "Radio"Type= "Radio"/>
<Input ID= "Text1" Name= "Text1" Type= "Text" />
 
<Input ID= "Submit1" Type= "Submit" Value= "Submit" />
 
<%}%>

 

Submit the form. Client changes are expected.

 

On the server side, the received data has changed. Multiple values are separated by commas (,), but the default values are "on"

 

 

After the single-choice and multiple-choice values are assigned separately,

Client changes

 

Changes on the server end, so that you can determine the user's operations

 

What if I remove the ID attribute?

 
 <% Using(Html. beginform ())
 
{%>
 
<InputName= "Checkbox"Type= "Checkbox" Value= "1"/>
<InputName= "Checkbox" Type= "Checkbox"Value= "2"/>
 
<InputName= "Checkbox"Type= "Checkbox" Value= "3"/>
 
<InputName= "Checkbox" Type= "Checkbox"Value= "4"/>
<InputName= "Checkbox"Type= "Checkbox"Value= "5"/>
 
<Input Name= "Radio"Type= "Radio" Value= "6"/>
 
<InputName= "Radio" Type= "Radio"Value= "7"/>
<InputName= "Radio"Type= "Radio" Value= "8" />
 
<InputName= "Text1" Type= "Text" />
 
<InputType= "Submit" Value= "Submit" />
 
<%}%>

Client changes are not changed

 

Summary: The name attribute is required for HTML form submission, and the ID attribute is optional.

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.