使用Javascript根据复选框切换div的可见性

时间:2014-07-24 10:17:46

标签: javascript html checkbox

我想在加载时隐藏所有div。然后,我想根据选中的复选框来显示/隐藏Div。

就我的例子而言,我有3辆跑车:

跑车1 =显示,无GPS。

跑车2 = GPS和显示屏。

跑车3 = GPS,无显示。

因此,当选择跑车时,它会显示包含所有跑车车型的'sportsCarAll'Div(同时隐藏所有其他Div)。

但当选择跑车+ GPS时,它会显示'sportsCarGps'Div并隐藏其他Div。

我是JavaScript的新手,所以任何帮助都会非常感激。

HTML

<div>
    <input type="checkbox" id="sportsCar" name="cartype">
    <input type="checkbox" id="gps" name="extraGps">
    <input type="checkbox" id="display" name="extraDisplay">
</div>
<div id="sportsCarAll">
    <h2>Sports Car 1, Sports Car 2 or Sports Car 3</h2>
</div>
<div id="sportsCarGps">
    <h2>Sports Car 2 or Sports Car 3</h2>
</div>
<div id="sportsCarDisplay">
    <h2>Sports Car 1 or Sports Car 2</h2>
</div>
<div id="sportsCarGpsAndDisplay">
    <h2>Sports Car 2</h2>
</div>

这是 Javascript 我正在尝试但无济于事。任何帮助将不胜感激。

   <script type="text/javascript" language="JavaScript">
       function hideDiv() {
        document.getElementById('sportsCarAll').style.visibility= 'hide';
        sportsCarGps.style.visibility = 'hide';
        document.getElementById('sportsCarDisplay').style.visibility = 'hide';
        document.getElementById('sportsCarGpsAndDisplay').style.visibility = 'hide';
        }

function showContent(){
if(document.getElementById('sportsCar').checked && 
document.getElementById('gps').checked &&
  document.getElementById('display').checked){
        document.getElementById('sportsCarGpsAndDisplay').style.visibility = 'show'
        document.getElementById('sportsCarAll').style.visibility = 'hide'
        document.getElementById('sportsCarGps').style.visibility = 'hide'
        document.getElementById('sportsCarDisplay').style.visibility = 'hide';
}
else if(document.getElementById('sportsCar').checked && 
document.getElementById('gps').checked){
        document.getElementById('sportsCarGpsAndDisplay').style.visibility = 'hide'
        document.getElementById('sportsCarAll').style.visibility = 'hide'
        document.getElementById('sportsCarGps').style.visibility = 'show'
        document.getElementById('sportsCarDisplay').style.visibility = 'hide';
}
             else if(document.getElementById('sportsCar').checked && 
document.getElementById('display').checked){
    document.getElementById('sportsCarGpsAndDisplay').style.visibility = 'hide'
        document.getElementById('sportsCarAll').style.visibility = 'hide'
        document.getElementById('sportsCarGps').style.visibility = 'hide'
        document.getElementById('sportsCarDisplay').style.visibility = 'show';
}
             else(document.getElementById('sportsCar').checked){
    document.getElementById('sportsCarGpsAndDisplay').style.visibility = 'hide'
        document.getElementById('sportsCarAll').style.visibility = 'show'
        document.getElementById('sportsCarGps').style.visibility = 'hide'
        document.getElementById('sportsCarDisplay').style.visibility = 'hide';
}
}

    </script>

1 个答案:

答案 0 :(得分:0)

没有读过你所有的代码 但这是一个简单的例子

<html>
    <head>
        <style type="text/css">
            #myData{visibility:hidden;}
        </style>
        <script language="JavaScript">

            function toggleVisibility(eventsender, idOfObjectToToggle){
                var myNewState = "hidden";
                if (eventsender.checked === true){
                    myNewState = "visible";
                }

                document.getElementById(idOfObjectToToggle).style.visibility = myNewState;
            }       
        </script>
    </head>
    <body>
        <input type="checkbox" id="myId" name="myName" onClick = "JavaScript:toggleVisibility(this,'myData');"/>
        <div name="myData" id="myData"> <h1>My Data</h1></div>
    </body>
</html>

我写了一个小例子来切换不同的组合 (警告!它只是快速而肮脏)

<html>
<head>
<style type="text/css">
    #myData{visibility:hidden;}
    #myData2{visibility:hidden;}
    #myData3{visibility:hidden;}
</style>
<script language="JavaScript">      

    function evaluateCheckboxState()
    {
        var itemToToggle = 0;
        if (document.getElementById("myId").checked) itemToToggle += 1;
        if (document.getElementById("myId2").checked) itemToToggle += 2;            
        return itemToToggle;
    }

    function toggleVisibility()
    {
        var myNewState = "hidden";
        var itemToShow = evaluateCheckboxState();
        var idToShow = "";
        //hide all divs                     
        document.getElementById("myData").style.visibility = myNewState;
        document.getElementById("myData2").style.visibility = myNewState;
        document.getElementById("myData3").style.visibility = myNewState;
        if (itemToShow === 0) return;

        switch (itemToShow)
        {
            case 1:
                idToShow = "myData";
                break;
            case 2:
                idToShow = "myData2";
                break;
            case 3:
                idToShow = "myData3";
                break;
            default:
                break;  
        }           

        myNewState = "visible";
        document.getElementById(idToShow).style.visibility = myNewState;
    }       
</script>
</head>
<body>
<input type="checkbox" id="myId" name="myName" onClick = "JavaScript:toggleVisibility();"/>
<input type="checkbox" id="myId2" name="myName2" onClick = "JavaScript:toggleVisibility();"/>
 <div name="myData" id="myData"> <h1>My Data1</h1></div>
 <div name="myData2" id="myData2"> <h1>My Data2</h1></div>
 <div name="myData3" id="myData3"> <h1>My Data3</h1></div>
</body>
</html>