Home >Web Front-end >HTML Tutorial >What is the function of the name attribute in HTML? What is the default name attribute?
This article mainly introduces the name attribute of the form in HTML, including basic definitions and example renderings, as well as explanations of the role of the name attribute in HTML, and examples of the name attribute in the form.
Let’s first understand the definition of the name attribute in the form:
The name attribute specifies the name of the input element.
The name attribute is used to identify the form data after it is submitted to the server
or reference the form data through JavaScript on the client.
Note: Only form elements with the name attribute set can pass their values when submitting the form.
<input name="value">
Look at an example to help you understand the above definition better
HTML form with two text fields and a submit button:
<form action="form_action.asp" method="get"> <p>name: <input type="text" name="fullname" /></p> <p>email: <input type="text" name="email" /></p> <input type="submit" value="Submit" /> </form>
What is the effect of the code? Look at the picture
Having said so much, everyone should have some understanding of the name attribute, so now let’s talk about the role of the name attribute in HTMLBar
If you use request.form("name") when submitting the form, you can get the value entered in your text box. If there are multiple controls on your page with the same name, then You can get an array using getelementsbyname. Also, if there are two radios on your page that allow users to choose gender, if you do not set the name attribute for these two buttons, you can try what effect it will have. Radio can be used to allow users to select gender. Generally, when selecting gender, only one can be selected. If you do not set the names of the two radios to be the same, then he will be able to select both male and female, so The name attribute is very important in the form.
Having said so much, let’s look at an example code of the role of the name attribute:
<html> <select> <option value="1">php中文网</option> <option value="2" selected="selected">百度</option> <option value="3">腾讯</option> </select> <form> <input type="checkbox" name="newsletter" checked="checked" value="Daily" />非常喜欢 <input type="checkbox" name="newsletter" value="Weekly" />喜欢 <input type="checkbox" name="newsletter" checked="checked" value="Monthly" />还行 </form> <input type="button" value="确定"/> <SPAN style="WHITE-SPACE: pre"> <input type="text" value="请输入原因"/> </SPAN> </html>
This picture What is the effect? Let us take a look.
The name attribute is the name of the control (multiple controls can have the same name), and the value is the name of the control. Value
Not all values of controls will be displayed
After defining the name and value of the control, you can obtain the control and its value on the server
I didn’t see it The name of submit does not mean that the browser ignores its name. It is also defined by the browser before submission. Its name and value can also be obtained on the server.
The control does not define name/value It can also be displayed. Its name/value is defined just for the convenience of receiving and distinguishing it on the server. Of course, the value of the button is not only to store its value, but also to display it.
Okay, that’s it. The entire content of the article is here. If you have any questions, please feel free to ask below
[Related recommendations]
What does the head tag in HTML mean? An article teaches you how to use the head tag correctly
The above is the detailed content of What is the function of the name attribute in HTML? What is the default name attribute?. For more information, please follow other related articles on the PHP Chinese website!