jQuery切换单选按钮上的背景颜色

时间:2014-03-04 05:32:45

标签: javascript jquery html css radio-button

我有两个设计为按钮的无线电控件。当我点击第一个单选按钮时,第二个的背景颜色应该改变,反之亦然。

HTML:

<input type="radio" id="boo1" name="boo" value="boo1"><label for="boo1"><span></span>boo1</label>
<input type="radio" id="boo2" name="boo" value="boo2"><label for="boo2"><span></span>boo2</label>

CSS:

body {
font-family: open sans, arial, sans-serif;
color: #ffffff;
margin: 0;
background: #3894db;
}
input[type="radio"] {
display: none;
}
input[type="radio"] + label {
cursor: pointer;
width: 50%;
height: auto;
font-size: 22px;
margin-top: 10px;
float: left;
color: #ffffff;
text-align: center;
vertical-align: middle;
padding-top: 2px;
padding-bottom: 2px;
}
/* boo1 */
#boo1 + label {
background: #e74c3c;
}
#boo1:checked + label {
background: #c0392b;
}
/* boo2 */
#boo2 + label {
background: #f1c40f;
}
#boo2:checked + label {
background: #f39c12;
}

的JavaScript?

$(document).ready(function() {
$("#boo1").click(function () {

});
});

7 个答案:

答案 0 :(得分:2)

if($('#boo1').is(':checked'))
{
     $('#boo2').css('color','#ff0000');
}

答案 1 :(得分:1)

如果您希望按钮在“非活动”时具有不同的背景颜色,则可以使用jQuery的css()功能,如下所示:

$(document).ready(function () {
    $("#boo1").click(function () {
        $("#boo1 + label").css("background-color", "#c0392b");
        $("#boo2 + label").css("background-color", "green");
    });
    $("#boo2").click(function () {
        $("#boo2 + label").css("background-color", "#f39c12");
        $("#boo1 + label").css("background-color", "blue");
    });
});

请参阅working jsFiddle

答案 2 :(得分:0)

尝试以下代码

$("#boo1").click(function (event) {

                $("#boo2").prop( "checked",true );
            });

            $("#boo2").click(function (event) {

                $("#boo1").prop( "checked",true );
            });

答案 3 :(得分:0)

尝试使用此javascript代码

$(document).ready(function () {
    $("input[name='boo']").click(function () {
        $(this).next().removeAttr('style');
        $("input[name='boo']").not(this).next().css('background-color', 'green');
    });
});

答案 4 :(得分:0)

试试这种方式

HTML CODE:

      <label for="boo1"><span><input type="radio" id="boo1" name="boo" value="boo1" /></span>boo1</label>
      <label for="boo2"><span><input type="radio" id="boo2" name="boo" value="boo2" /></span>boo2</label>

JQUERY CODE:

        $('input[type=radio]').on('click', function () {
             $('input[type=radio]').not(this).closest('label').css('background', 'blue');
             $(this).closest('label').css('background', 'red');
        });

现场演示:

快乐编码:) http://jsfiddle.net/dreamweiver/py2BM/14/

答案 5 :(得分:0)

您可以使用纯CSS

实现这一目标
/* boo1 */
#boo1 + label {
background: #e74c3c;
}
#boo1:checked ~ label[for="boo2"] {
background: #c0392b;
}
#boo1:checked ~ label[for="boo1"]{
    background: #f39c12;
}
/* boo2 */
#boo2 + label {
background: #f1c40f;
}
#boo2:checked ~ label[for="boo1"] {
background: #f39c12;
}

工作演示:http://jsfiddle.net/HarishBoke/S6z8Z/

希望这就是你要找的东西!

答案 6 :(得分:0)

简单优化的解决方案:

HTML代码:

<!-- Fieldset Wrap: begins -->
<div class="fieldset-wrap">

    <!-- Fieldset: begins -->
    <span class="fieldset orange">
        Label 1
    </span>
    <!-- Fieldset: ends -->

    <!-- Fieldset: begins -->
    <span class="fieldset green">
       Label 2
    </span>
    <!-- Fieldset: ends --> 

</div>
<!-- Fieldset Wrap: ends -->

<强> CSS:

.fieldset-wrap {
    overflow: hidden; 
    font-size:0px;
}

.fieldset { 
    display: inline-block; 
    width: 200px;
    padding: 5px 0;
    text-align: center;
    cursor: pointer;
    font-family: verdana;
    font-size: 14px;
 }

.fieldset { margin-left: 1px; }
.fieldset:first-child { margin-left: 0px; }

.orange,
.green,
.checked{ color: #fff; }

.orange { background-color : orange; }
.green { background-color : green; }
.checked { background-color : blue; }

<强> jQuery的:

$('.fieldset').on('click', function(){
    $(this)  
    .addClass('checked')
    .siblings()
    .removeClass('checked');
});

DEMO

相关问题