复选框输入元素中`name` 属性的用途是什么? [英] What is the purpose of the `name` attribute in a checkbox input element?

查看:44
本文介绍了复选框输入元素中`name` 属性的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了许多有关 XHTML 中复选框输入的在线文档.谁能解开我的疑惑?这个 name 字段实际上代表什么?

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?

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.

推荐答案

不要因为 name="checkbox" 而混淆.从逻辑上讲,它可以是 name="drink"type=checkbox.

Dont be confused because of name="checkbox". It could more logically be 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 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.

更新:

在接收端,例如,如果您使用 JSP - 所选复选框的值将作为 request.getParameterValues("drink")request.getParameterValues("checkbox") 在您的实际情况中.这是使用 name 属性的地方.

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.

这篇关于复选框输入元素中`name` 属性的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆