简体   繁体   English

复选框输入元素中的“名称”属性的用途是什么?

[英]What is the purpose of the `name` attribute in a checkbox input element?

I went through many online documents for the checkbox input in XHTML.对于 XHTML 中的复选框输入,我浏览了许多在线文档。 Can anyone clear my doubt?谁能解开我的疑惑? What does this name field actually stand for?这个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.对此有点困惑。

Dont be confused because of name="checkbox" .不要因为name="checkbox"而感到困惑。 It could more logically be name="drink" and type=checkbox .从逻辑上讲,它可能是name="drink"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 checkboxes will 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.在接收端,例如,如果您使用 JSP - 所选复选框的值将在您的实际情况下以request.getParameterValues("drink")request.getParameterValues("checkbox")的形式提供。 This is where the name attribute is used.这是使用name属性的地方。

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.唯一需要多个具有相同nameinput的地方是当它们是单选按钮时,在这种情况下,它用于指示其中哪一个属于同一组,因此只能选择其中一个一次。

The name attribute is used to identify a checkbox. name属性用于标识复选框。 Which you could parse into a object like so {checkboxName1: 'checkboxValue2', checkboxName2: 'checkboxValue2'}您可以将其解析为像这样的对象{checkboxName1: 'checkboxValue2', checkboxName2: 'checkboxValue2'}

“名称”与数据库记录相同,每个字段都应该有一个名称,所以当您点击提交时,数据将记录到数据库中~~~~~

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM