Html 复选框输入元素中`name` 属性的用途是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3626883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the purpose of the `name` attribute in a checkbox input element?
提问by Nagaraj Tantri
I went through many online documents for the checkbox input in XHTML. Can anyone clear my doubt? What does this name
field actually stand for?
我浏览了许多有关 XHTML 中复选框输入的在线文档。有人能解开我的疑惑吗?这个name
字段实际上代表什么?
Milk: <input type="checkbox" name="checkbox" value="Milk">
Chocolate: <input type="checkbox" name="checkbox" value="chocolate">
Cold Drink: <input type="checkbox" name="checkbox" value="Cold Drink">
I thought it was an identifier for that particular checkbox, which can later be used in other file by just referring their name, but given that all the checkbox had same name, why even specify it? A little confused of this.
我认为它是该特定复选框的标识符,以后可以通过引用它们的名称在其他文件中使用它,但是鉴于所有复选框具有相同的名称,为什么还要指定它?对此有点困惑。
回答by JoseK
Dont be confused because of name="checkbox"
. It could more logically be name="drink"
and type=checkbox
.
不要因为name="checkbox"
. 它可以更合乎逻辑地是name="drink"
and type=checkbox
。
In the above case, you have multiple checkboxes with the same name. When several checkboxes have the same name, the form will send a group of values to the server in the request. Note: only the values of the checked checkboxeswill be sent to the server.
在上述情况下,您有多个具有相同名称的复选框。当多个复选框具有相同名称时,表单将在请求中向服务器发送一组值。注意:只有选中复选框的值才会发送到服务器。
Ideally these are used to allow multiple choice questions where more than one answer is allowed. As opposed to radio buttons, where only one answer is allowed among the options.
理想情况下,这些用于允许允许多个答案的多项选择题。与单选按钮相反,单选按钮在选项中只允许一个答案。
Update:
更新:
On the receiving side, if you're using JSP for example - the values of the selected checkboxes will be available as request.getParameterValues("drink")
or request.getParameterValues("checkbox")
in your actual case. This is where the name
attribute is used.
在接收端,如果你使用JSP例如-选择复选框的值将作为request.getParameterValues("drink")
或request.getParameterValues("checkbox")
在您的实际情况。这是使用name
属性的地方。
回答by Yi Jiang
The name attribute is used to reference form data after it's submitted, and to reference the data using JavaScript on the client side.
name 属性用于在提交后引用表单数据,并在客户端使用 JavaScript 引用数据。
Source: http://reference.sitepoint.com/html/input/name
来源:http: //reference.sitepoint.com/html/input/name
Basically, what you've described. When the form is submitted, you can access the values of the form elements through the name
you ascribe to them.
基本上,你所描述的。提交表单后,您可以通过name
您归属于表单元素的值来访问表单元素的值。
The only place where you would want to have multiple input
s with the same name
is when they are radio buttons, in which case it is used to indicate which one of them belongs to the same group and thus only one of which can be selected at a time.
您希望多个input
s 具有相同的唯一地方name
是当它们是单选按钮时,在这种情况下,它用于指示其中哪一个属于同一组,因此一次只能选择其中一个.
回答by Matthew Ma
the "name" is same with the databse record, every field should have a name, so when you click the submit, the data will recorded to the database~~~~~
“名称”与数据库记录相同,每个字段都应该有一个名称,所以当您点击提交时,数据将记录到数据库中~~~~~