我知道有时将标签与复选框相关联是很好的做法:

<input id="something" type="checkbox" value="somevalue" />
<label for="something">this is label text</label>

..but do I have to use an id to associate it?
The main reason I even care is because I like being able to click a label to toggle the checkbox value, but don't like the idea of using an id for something so simple.

I guess I could use jQuery toggle the previous element (checkbox) of a clicked label, but maybe there is something simpler I'm missing. https://stackoverflow.com/a/2720771/923817 looked like a solution, but the user said it doesn't work in IE.

推荐答案

Yes, place the input inside the label.

<label><input type=checkbox name=chkbx1> Label here</label>

参见HTML规范中的100.

Jquery相关问答推荐

如何在不使用点击事件 bootstrap 程序模式的情况下使用弹出窗口 bootstrap 程序显示用户详细信息?

我正在try 使用 Jquery 打开与帖子相关的特定 comments

为什么 .join() 不能与函数参数一起使用?

Javascript 仅打印 iframe 内容

将 HTML5 Canvas 转换为要上传的文件?

如何使用Angular 检测浏览器后退按钮单击事件?

Ajax 更新后在 jQuery 中重新绑定事件(更新面板)

如何使用jQuery动态设置宽度和高度

jQuery Force 为 iframe 设置 src 属性

addClass - 可以在同一个 div 上添加多个类吗?

jquery变量语法

为什么不推荐$().ready(handler)?

jQuery UI 中的 disableSelection 用于什么?

jQuery Uncaught TypeError: 对象 [object Window] 的属性$不是函数

从 Select 元素中获取选定的选项

jQuery表格行中的每个循环

如何测试 URL 字符串是绝对的还是相对的?

变量首字母大写

bootstrap 日期和时间 Select 器

catch forEach 最后一次迭代