简体   繁体   English

选中复选框标签时未设置样式

[英]Checkbox label not styling when checked

I am trying to create a check box label where the checkbox itself is hidden but the label isn't therefore clicking on the label, checks the checkbox, even if not shown. 我试图创建一个复选框标签,其中复选框本身是隐藏的,但是该标签因此并未单击标签,请选中该复选框,即使未显示也是如此。

What I want to happen is when it's checked, the label stays red, and for this i assigned a class for checkbox active, but its not working. 我想要发生的是,当它被选中时,标签保持红色,为此我为复选框分配了一个活动类,但它不起作用。 It works fine on hover just not on active. 它在悬停时很好,只是不在活动时。 When I click it the checkbox checks but the label does not read the active class. 当我单击它时,复选框会选中,但标签不会读取活动的类。

Any ideas? 有任何想法吗?

I use the following html: 我使用以下html:

<div class="gl_delete">
    <div class="deleteCB">
        <input id="uImgId47" type="checkbox" name="uImgId[]" value="47"></input>
        <input type="hidden" name="filename[]" value="6_246f0548d62b9b9cc19210389ef09472.jpg"></input>
        <label onclick="$('#delHolder').show()" for="uImgId47"> X </label>
    </div>
</div>

And the following css 和以下CSS

.deleteCB {
width: 20px;    
margin: 20px auto;
}

.deleteCB label, .deleteCB label:after {
cursor: pointer;
position: absolute;
font-weight:bold;
font-size:14px;
content: 'x';
line-height:14px;
position: absolute;
border-radius:6px;
padding-left:9px;
padding-top:3px;
color:#fff;
width: 17px;
height: 21px;
background: #ccc;
border:1px solid #ccc;
top: -1px;
right: -1px;
padding-bottom:0;
}

.deleteCB label:after {
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
filter: alpha(opacity=0);
opacity: 0;
color:#fff;
background: #cc3333;
border:1px solid #cc3333;
}

.deleteCB label:hover::after {
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=30)";
filter: alpha(opacity=100);
opacity: 1;
}

.deleteCB input[type=checkbox]:checked + label:after {
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
filter: alpha(opacity=100);
opacity: 1;
}

/* hide checkbox */
input[type=checkbox] {
visibility: hidden;
}

The selector .deleteCB input[type=checkbox]:checked + label:after uses an adjacent sibling selector + which means it is picking the label directly after the input[type=checkbox]:checked element 选择器.deleteCB input[type=checkbox]:checked + label:after使用相邻的同级选择器+ ,这意味着它直接在input[type=checkbox]:checked元素之后选择标签。

This does not work because there is another input element between the checkbox and label elements - so your selector would actually be: 这不起作用,因为复选框和标签元素之间还有另一个input元素-因此您的选择器实际上是:

.deleteCB input[type=checkbox]:checked + input[type=hidden] + label:after

Instead you can also use the general sibling combinator which selects all elements that appear after: 相反,您还可以使用通用的同级组合器,该组合器选择出现在以下位置的所有元素:

.deleteCB input[type=checkbox]:checked ~ label:after

Here is a little more information about sibling selectors: CSS Tricks: Child and Sibling Selectors 以下是有关同级选择器的更多信息: CSS技巧:子级和同级选择器

If I'm understanding you right, I'd simply suggest adding a class to your label via JavaScript rather than relying solely on CSS. 如果我理解正确,我只是建议您通过JavaScript将类添加到您的标签中,而不是仅依赖CSS。 You've got an onclick handler on your label already, so I'd suggest making a function to call when that's clicked, which handles the show() method you have already, plus adds a class to your label. 您的标签上已经有一个onclick处理程序,因此建议您创建一个在单击时调用的函数,该函数处理您已经拥有的show()方法,并向标签中添加一个类。

For example, the HTML: 例如,HTML:

<label id="labelImgId47" onclick="onLabelClick()" for="uImgId47">...</label>

...and the JavaScript: ...以及JavaScript:

function onLabelClick() {
    $('#delHolder').show();
    $('#labelImgId47').addClass('active');
}

If the user can toggle the checkbox on/off, you can add in conditional logic to check whether its checked or not, and use the jQuery removeClass() function when you need to show the box as unchecked. 如果用户可以打开/关闭复选框,则可以添加条件逻辑以检查其是否已选中,并在需要将复选框显示为未选中时使用jQuery removeClass()函数。

Then in your CSS, you can just style your label with active class: 然后在CSS中,您可以仅使用active类设置标签样式:

.deleteCB label.active {
    // Your styles here
}

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

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