首页 > 代码库 > 用JS把复选框做成单选框,左显示div,右隐藏div

用JS把复选框做成单选框,左显示div,右隐藏div

<input type="checkbox" name="checkname" onclick="check(this)"/>左
<input type="checkbox" name="checkname" onclick="check(this)"/>右

<div id="show" style="display:block;">显示</div>
<div id="hidden" style="display:none;">隐藏</div>

<script type="text/javascript">
function check(obj){
    var checks = document.getElementsByName("checkname");
    if(obj.checked){
        for(var i=0;i<checks.length;i++){
        checks[i].checked = false;
        }
        obj.checked = true;
    }else{
        for(var i=0;i<checks.length;i++){
        checks[i].checked = false;
        }
    }    
    document.getElementById(‘show‘).style.display = document.getElementById(‘show‘).style.display=="none"?"block":"none";
    document.getElementById(‘hidden‘).style.display = document.getElementById(‘hidden‘).style.display=="none"?"block":"none";    
}
</script>

 

用JS把复选框做成单选框,左显示div,右隐藏div