I have a list of possible products a user can buy. For doing it, I use ul tag combined with li. Every element has a checkbox that allows the user to choose if select the product or not.

Some products have related information. In order to describe this, I would like to store the data inside an hidden input. But because the selection and the information are related to a product I thought to use a label that contains the checkbox and the hidden input.

Something like

<label class="product">
  <input class="product-checkbox" name="product1" type="checkbox">
  <input type="hidden" name="product1-information" value="{...}" />
  <span class="product-name">Product1</span>
</label>

If I understood correctly, a label cannot refer to an hidden input but in the above example, accordingly to the w3c, the labeled control is the checkbox.

Anyway I'm wandering if a label can contains checkbox and an hidden input.

So, is the above snipper correct?

推荐答案

You're right on both counts. From MDN

The form control that a label is labeling is called the labeled control of the label element. Multiple labels can be associated with the same form control:

<label for="username">Enter your username:</label>
<input id="username">
<label for="username">Forgot your username?</label>

Elements that can be associated with a <label> element include <button>, <input> (except for type="hidden"), <meter>, <output>, <progress>, <select> and <textarea>.

(emphasis mine)

That makes it pretty clear to me that (a) multiple things inside a <label> block is entirely acceptable, and (b) hidden inputs will not be considered targets of the label.

Html相关问答推荐

VBA从公共Google Drive地址下载文档-.Click事件(?)

为所有必填字段添加所需的占位符文本(Angular Material)

在Chrome中使用手写笔时,滚动条方向反转

使用HTML进行DAX测量

当我关闭时,导航栏跳到了新的生产线

不能以Angular 更改输入的S边框 colored颜色

Angular -表和源映射中未显示的数据错误

响应网格,响应调整大小以适应父div,保留父div S自己的响应高度

Bootstrap 5.3.2模式页脚左填充还是左边距?

如何使div填充父项的剩余高度

如何制作带有粘性头和脚的可滚动车身

使用bash从html表格中提取表格

如何在悬停时使矩形按钮上的边框变圆

如何居中此按钮,即使它已经在计算机分辨率中居中

如何将内容放置在侧边栏旁边?

ul li 右侧的 Bootstrap 加载微调器

是否可以将文本添加到表格边框?

提交前的验证表单未显示任何验证消息?

悬停一个 div 时更改所有其他 div 上的字体 colored颜色

如何创建 宏?