如何更改复选框的背景颜色?

时间:2014-09-10 06:06:24

标签: jquery

CSS:

#partoption2 {
    background: none repeat scroll 0 0 #FF0000;
    height: 16px;
    position: relative;
    top: 3px;
    width: 16px;
}

HTML:

<input type="checkbox" name="parttwo" id="partoption2" value="206365-KT:" >manoji

嗨朋友在这里我需要复选框的背景颜色为绿色,当我取消选中时我检查它应该变红。谁能给我一个解决方案?

http://jsfiddle.net/5txp5g5u/1/

2 个答案:

答案 0 :(得分:4)

http://codepen.io/dcdev/full/toBzb/

未选中时为绿色,选中时为红色..这可以通过实际隐藏默认复选框并将标签样式设置为隐藏复选框来显示为复选框。

label {
    position: absolute;
    width: 20px;
    height: 20px;
    background-color: green;
    -webkit-transition: background-color 1s ease-out 1s;
    -moz-transition: background-color 1s ease-out 1s;
    -o-transition: background-color 1s ease-out 1s;
    transition: background-color 1s ease-out 1s;
}

.checkbox_red input[type=checkbox]:checked + label {
    background-color:red;
    -webkit-transition: background-color 1s ease-out 1s;
    -moz-transition: background-color 1s ease-out 1s;
    -o-transition: background-color 1s ease-out 1s;
    transition: background-color 1s ease-out 1s;
}

.checkbox_red label:after {
    position: absolute;
    bottom: 8px;
    width: 18px;
    height: 10px;
    opacity: 0;
    content: '';
    background: transparent;
    border: 3px solid #000;
    border-top: none;
    border-right: none;
    -webkit-transform: rotate(-50deg);
    -moz-transform: rotate(-50deg);
    -ms-transform: rotate(-50deg);
    -o-transform: rotate(-50deg);
    transform: rotate(-50deg);
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
    filter: alpha(opacity=0);
}
.checkbox_red input[type=checkbox] {
    visibility: hidden;
}
.checkbox_red input[type=checkbox]:checked + label:after {
    opacity: 1;
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
    filter: alpha(opacity=100);    
}

和html

<div class="checkbox_red">
  <input type="checkbox" value="none" id="checkbox_red1" name="check">
  <label for="checkbox_red1"></label>
</div>

答案 1 :(得分:0)

试试这个..我希望你能找到解决问题的方法。

******html
<p><input type="checkbox" name="1" class="styled green"/> (green)</p>
<p><input type="checkbox" name="2" class="styled red" checked/> (red)</p>
<p><input type="checkbox" name="3" class="styled purple" /> (purple)</p>

********javascript

$(function() {

    $('input[type=checkbox]').each(function() {
        var span = $('<span class="' + $(this).attr('type') + ' ' + $(this).attr('class') + '"></span>').click(doCheck).mousedown(doDown).mouseup(doUp);
        if ($(this).is(':checked')) {
            span.addClass('checked');
        }
        $(this).wrap(span).hide();
    });

    function doCheck() {
        if ($(this).hasClass('checked')) {
            $(this).removeClass('checked');
            $(this).children().prop("checked", false);
        } else {
            $(this).addClass('checked');
            $(this).children().prop("checked", true);
        }
    }

    function doDown() {
        $(this).addClass('clicked');
    }

    function doUp() {
        $(this).removeClass('clicked');
    }
});


****css

.checkbox, .radio {
    width: 19px;
    height: 20px;
    padding: 0px;
    background: url("http://i48.tinypic.com/raz13m.jpg");
    display: block;
    clear: left;
    float: left;
 }

.checked {
     background-position: 0px -50px;   
}

.clicked {
     background-position: 0px -25px;
}

.clicked.checked {
     background-position: 0px -75px;
}


.green {
    background-color: green;
 }

 .red {
    background-color: red;
 }

.purple {
    background-color: purple;
 }