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

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

问题描述

我浏览了许多在XHTML中的复选框输入的在线文档。谁能清除我的怀疑?这个 name 字段实际上代表什么?

 牛奶:< input type =checkboxname =checkboxvalue =Milk> 
巧克力:< input type =checkboxname =checkboxvalue =chocolate>
冷饮:< input type =checkboxname =checkboxvalue =冷饮>

我认为这是特定复选框的标识符,稍后可以在其他文件中使用引用他们的名字,但是鉴于所有的checkbox都有相同的名字,为什么还要指定呢?有点困惑。

解决方案

不要因为 name =checkbox。更符合逻辑的是 name =drink type = checkbox



在上面的例子中,你有多个相同名称的复选框。当多个复选框具有相同的名称时,表单将在请求中向服务器发送一组值。注意:只有已选中的复选框的值会发送到服务器。



理想情况下,这些选项用于允许多个选择题,一个答案是允许的。
相对于单选按钮,其中的选项只允许一个答案。



更新:
$ b

在接收端,如果您使用JSP,所选复选框的值将作为 request.getParameterValues(drink) request.getParameterValues(checkbox)。这是使用 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.

解决方案

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.

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.

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

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