[英]Check/uncheck checkbox without using JavaScript/jQuery
Is it possible to toggle the state (check/uncheck) of an input type checkbox without using JavaScript/jQuery ie only through HTML and CSS? 是否可以在不使用JavaScript / jQuery的情况下切换输入类型复选框的状态(选中/取消选中),即仅通过HTML和CSS?
Say I want to toggle it on click on a label next to it. 假设我想点击旁边的标签来切换它。
If yes, how can we achieve that? 如果是,我们如何实现这一目标?
If you need to check/uncheck the input from a <label>
, set an id
on the input, and use the label's for
attribute to connect them. 如果需要检查/取消选中
<label>
的输入,请在输入上设置id
,并使用label的for
属性连接它们。
<input type="checkbox" id="chk" /> <label for="chk">Click to check/uncheck</label>
Another option is to use the label as a container to the input: 另一种选择是使用标签作为输入的容器:
<label><input type="checkbox" /> Click to check/uncheck</label>
This answer is for the question before it was updated! 这个答案是在更新之前的问题!
Only using a hardcoded attribute checked
: 仅使用已
checked
的硬编码属性:
<input type="checkbox" checked />
If you want to trigger it from code, it's impossible without JS/jQuery. 如果你想从代码中触发它,没有JS / jQuery是不可能的。
label and input are meant to work together :) 标签和输入是为了一起工作:)
see : https://www.w3.org/wiki/HTML/Elements/label 请参阅: https : //www.w3.org/wiki/HTML/Elements/label
The caption can be associated with a specific form control:
标题可以与特定的表单控件相关联:
Using for attribute [Example A]
用于属性[示例A]
By putting the form control inside the label element itself.
将表单控件放在标签元素本身内。 [Example B]
[例B]
<label for="a">toggle state</label><input type="checkbox" id="a"/>
YES , You Can. 是的你可以。 Use this code
使用此代码
<label for="MyCheckBox">Click Me !</label>
<input type="checkbox" id="MyCheckBox" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.