如何创建带有可点击标签的复选框?

HTML

千羽

2020-03-16

如何创建带有可单击标签的HTML复选框(这意味着单击标签可以打开/关闭该复选框)?

第1751篇《如何创建带有可点击标签的复选框?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

9个回答
NearLHarry 2020.03.16

In Angular material label with checkbox

<mat-checkbox>Check me!</mat-checkbox>
Pro村村凯 2020.03.16

Use the label element, and the for attribute to associate it with the checkbox:

<label for="myCheckbox">Some checkbox</label> <input type="checkbox" id="myCheckbox" />

西里神乐 2020.03.16
<label for="my_checkbox">Check me</label>
<input type="checkbox" name="my_checkbox" value="Car" />
卡卡西Tom 2020.03.16

This should help you: W3Schools - Labels

<form>
  <label for="male">Male</label>
  <input type="radio" name="sex" id="male" />
  <br />
  <label for="female">Female</label>
  <input type="radio" name="sex" id="female" />
</form>
LEYPro达蒙 2020.03.16

它也可以工作:

<form>
    <label for="male"><input type="checkbox" name="male" id="male" />Male</label><br />
    <label for="female"><input type="checkbox" name="female" id="female" />Female</label>
</form>
小卤蛋凯 2020.03.16

您还可以使用CSS伪元素分别从复选框的所有value属性中选择并显示标签。
编辑:这仅适用于基于Webkit和基于眨眼的浏览器(Chrome(ium),Safari,Opera ....)以及大多数移动浏览器。这里没有Firefox或IE支持。
仅在将webkit / blink嵌入到您的应用程序中时,这才有用。

<input type="checkbox" value="My checkbox label value" />
<style>
[type=checkbox]:after {
    content: attr(value);
    margin: -3px 15px;
    vertical-align: top;
    white-space:nowrap;
    display: inline-block;
}
</style>

所有伪元素标签将是可单击的。

演示:http://codepen.io/mrmoje/pen/oteLl,+ 它的要点

小小村村 2020.03.16
<label for="myInputID">myLabel</label><input type="checkbox" id="myInputID" name="myInputID />
猿米亚 2020.03.16
<label for="vehicle">Type of Vehicle:</label>
<input type="checkbox" id="vehicle" name="vehicle" value="Bike" />
小小西门 2020.03.16

只要确保标签与输入关联即可。

<fieldset>
  <legend>What metasyntactic variables do you like?</legend>

  <input type="checkbox" name="foo" value="bar" id="foo_bar">
  <label for="foo_bar">Bar</label>

  <input type="checkbox" name="foo" value="baz" id="foo_baz">
  <label for="foo_baz">Baz</label>
</fieldset>

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android