1

我有三个 div 当我想使用复选框时我应该有新的 div 如果我检查 car1 而未选中 car2 我应该有红色汽车但如果检查 car1 而检查 car2 我应该有黑色汽车

<div id="carblack">
    car black
</div>
<div id="carred"  style="display:none">
    car red
</div>
<div id="carblue"  style="display:none">
    car blue
</div>
<input type="checkbox" id="car1" name="vehicle" value="red" checked/>
<input type="checkbox" id="car2" name="vehicle1" value="blue"  checked/>
$('#car1').change(function () {

    if($(this).attr("checked")){
        document.getElementById("carred").style.display = "block";
        document.getElementById("carblack").style.display = "none";
    } else{
        document.getElementById("carblack").style.display = "block";
        document.getElementById("carred").style.display = "none";
    }
});

$('#car2').change(function () {

    if($(this).attr("checked")){
        document.getElementById("carblue").style.display = "block";
        document.getElementById("carblack").style.display = "none";
    } else{
        document.getElementById("carblack").style.display = "block"; 
        document.getElementById("carblue").style.display = "none";
    }
});
4

2 回答 2

1

2 收音机检查或没有检查 - 黑色?

$(function(){
    var cars = $('div[id^="car"]'),
        inputs = $('input[type="checkbox"]');

    inputs.change(function(){
        cars.hide();
        if(inputs.filter(':checked').length === 1)
        {
           cars.filter('#car' + $(this).val()).show();
        }
        else {
           cars.filter('#carblack').show();
        }
    });
});

演示:http: //jsfiddle.net/jAxHT/

于 2012-09-23T07:08:27.787 回答
0
$('#car1').change(function () {

if($(this).attr("checked")){
    if($(this).next().attr("checked")){
    document.getElementById("carblack").style.display = "block";
    document.getElementById("carred").style.display = "none";
     document.getElementById("carblue").style.display = "none";
    }else{
    document.getElementById("carred").style.display = "block";
    document.getElementById("carblack").style.display = "none";
}
} else if($(this).next().attr("checked")){
    document.getElementById("carblack").style.display = "none";
    document.getElementById("carblue").style.display = "block";
}else{
       document.getElementById("carblack").style.display = "block";
    document.getElementById("carred").style.display = "none";
     document.getElementById("carblue").style.display = "none";        }
});

$('#car2').change(function () {

if($(this).attr("checked")){
     if($(this).prev().attr("checked")){
     document.getElementById("carblack").style.display = "block";
     document.getElementById("carred").style.display = "none";
     document.getElementById("carblue").style.display = "none";

    }else{
    document.getElementById("carblue").style.display = "block";
    document.getElementById("carblack").style.display = "none";
    }
 } else if($(this).prev().attr("checked")){
    document.getElementById("carblack").style.display = "none"; 
    document.getElementById("carred").style.display = "block";
 }else{
       document.getElementById("carblack").style.display = "block";
    document.getElementById("carred").style.display = "none";
     document.getElementById("carblue").style.display = "none";        }
});​
于 2012-09-23T06:45:39.927 回答