简体   繁体   English

在选中单选按钮时显示不同的背景CSS和敲除

[英]displaying a different background when a radio button is checked css and knockout

I've seen some tricks to change the background color (or other css attributes) on a group of radio buttons. 我已经看到了一些技巧,可以在一组单选按钮上更改背景颜色(或其他CSS属性)。 Here is some html 这是一些HTML

<div class="myclass col-xs-3">
    <input type="radio" name="mygroup" value="one" data-bind="checked: SelectedAttributeValueId" />
</div>
<div class="myclass col-xs-3">
    <input type="radio" name="mygroup" value="two" data-bind="checked: SelectedAttributeValueId" />
</div>
<div class="myclass col-xs-3">
    <input type="radio" name="mygroup" value="three" data-bind="checked: SelectedAttributeValueId" />
</div>

I've tried things like: 我已经尝试过类似的事情:

.myclass input[type="radio"]:checked{
    background-color:#f2f2f2;   
}

and

.myclass :checked{
    background-color:#f2f2f2;
}

here is a fiddle link. 这是一个小提琴链接。 I am using knockout, so maybe this is the tool I should use to style the <div> elements? 我正在使用淘汰赛,所以也许这是我应该用来设置<div>元素样式的工具?

All input is appreciated, I would prefer not to use jquery or javscript here (although knockout is okay) 感谢所有输入,我不希望在此处不使用jquery或javscript(尽管可以删除)

It is not possible to style the radio buttons circle. 无法设置单选按钮圆圈的样式。
However, you can use pseudo-elements (in this case :before ) to render a box around the radio button, then style it in CSS. 但是,您可以使用伪元素(在本例中为:before )在单选按钮周围呈现一个框,然后在CSS中对其进行样式设置。

 input[type="radio"] { display: inline-block; position: relative; width: 25%; margin: 0; } input[type="radio"]:before { content: ''; position: absolute; z-index: -1; top: -.5em; right: 0; bottom: -.5em; left: 0; border: 1px solid black; background-color: #0073ae; } input[type="radio"]:checked:before { background-color: #f2f2f2; } 
 <input type="radio" name="mygroup" value="one" /><input type="radio" name="mygroup" value="two" /><input type="radio" name="mygroup" value="three"/> 

Here's a solution via jquery. 这是通过jquery的解决方案。

$('[type=radio]').click(function(){ 
    if($(this).val() == "one") {
        $('.myclass').css("background-color", "yellow");
    }
    //...two...three
});

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

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