gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:04:42 25 4
gpt4 key购买 nike

我希望所有 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>

最佳答案

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

<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>

关于javascript - 使用 Javascript 根据复选框切换 div 的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24931028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com