简体   繁体   English

使用 css 选择器禁用与复选框关联的标签

[英]Disabled label associated with checkbox using css selector

I want to disable the label associated with checkbox.我想禁用与复选框关联的标签。 The code snippets are below:代码片段如下:

code:代码:

<label for="label1" class="form-checkbox-left"><input type="checkbox" name="labelname" id="label1" value="0" style="min-width: 20px;" disabled>Name 1</label>

css : .form-checkbox-left input[type=checkbox]:disabled { color:#ccc; } css : .form-checkbox-left input[type=checkbox]:disabled { color:#ccc; } .form-checkbox-left input[type=checkbox]:disabled { color:#ccc; }

Somehow it is not working.不知何故它不起作用。

Please help

You can opt to use the element+element Selector.您可以选择使用元素+元素选择器。 You need to place the input before the label, however您需要将输入放在标签之前,但是

 input[type=checkbox]:disabled+label { color: #ccc; }
 <input type="checkbox" name="labelname" id="label1" value="0" style="min-width: 20px;" disabled> <label for="label1" class="form-checkbox-left">Name 1</label>

you can try to do with jquery using if ($('label1'.hasAttribute('disabled')){ $('[for="label1"]').attr('disabled', 'disabled'); }你可以尝试使用if ($('label1'.hasAttribute('disabled')){ $('[for="label1"]').attr('disabled', 'disabled'); }

at [for="label1"] you can put the id of lable ("if you want to")在 [for="label1"] 处,您可以放置​​标签的 ID(“如果您愿意”)

I have just double-checked with Firefox, and adding any sort of a style definition to a checkbox doesn't change any aspect of its appearance.我刚刚对 Firefox 进行了仔细检查,向复选框添加任何类型的样式定义都不会改变其外观的任何方面。 It seems that you are out of luck here and would have to take a different approach by hiding the checkbox itself (not with display: none; , but with visibility: hidden; instead) and then add auxiliary CSS to actually customize its appearance.看起来你在这里运气不好,必须采取不同的方法来隐藏复选框本身(不是使用display: none; ,而是使用visibility: hidden;代替),然后添加辅助 CSS 来实际自定义其外观。

An example on how to do this can be found on http://cssdeck.com/labs/css-checkbox-styles可以在http://cssdeck.com/labs/css-checkbox-styles上找到有关如何执行此操作的示例
Maybe that helps you derive a method that suits you best.也许这有助于您推导出最适合您的方法。

EDIT编辑

If you are attempting to style the parent label of the checkbox, you are out of luck, because parent element selectors are not implemented in CSS so far, although a proposal has been made (see https://shauninman.com/archive/2008/05/05/css_qualified_selectors for details).如果您尝试为复选框的父标签设置样式,那您就不走运了,因为到目前为止,CSS 中还没有实现父元素选择器,尽管已经提出了建议(请参阅https://shauninman.com/archive/2008 /05/05/css_qualified_selectors了解详情)。

EDIT 2编辑 2

You could try this by moving the label definition behind the checkbox definition:您可以通过将标签定义移到复选框定义后面来尝试此操作:

<input type="checkbox" name="labelname" id="label1" value="0" style="min-width: 20px;" disabled><label for="label1" class="form-checkbox-left">Name 1</label>

Then your CSS should look like this to facilitate the change:然后你的 CSS 应该看起来像这样以促进更改:

input[type="checkbox"][disabled] + label {
  color: #ccc;
  }

This in turn modifies the text of your label.这反过来会修改标签的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM