我正在尝试制作一个带有单选按钮的表单,该表单根据第一个问题中的选择显示不同的问题。
当第一个问题选择一个选项时,第二个问题显示正常。但是当您更改第一个问题的答案时,第二个问题不会隐藏。
例如: 您有一个带有两个选项的单选按钮: * 车 * 自行车
当您单击“汽车”时,会出现有关汽车的第二个问题。但是当用户犯了一个错误并将单选按钮更改为“自行车”时。关于自行车的第二个问题显示了,但是关于汽车的问题没有隐藏,并且都显示了。
function ShowCar() {
document.getElementById('DivCar').style.display = "";
document.getElementById('IpCar').disabled = false;
document.getElementById('DivBike').style.display = "none";
document.getElementById('IpBike').disabled = true;
}
function ShowBike() {
document.getElementById('DivBike').style.display = "";
document.getElementById('IpBike').disabled = false;
document.getElementById('DivCar').style.display = "none";
document.getElementById('IpCar').disabled = true;
}
<div id="prod">
<table width="600" border="0" cellspacing="0" cellpadding="5">
<tr>
<td>Car or Bike?</td>
<td>
<form>
<input type="radio" name="product" id="Car" value="Car" onChange="ShowCar()" /> Car<br>
<input type="radio" name="product" id="Bike" value="Bike" onChange="ShowBike()" /> Bike<br>
</form>
</td>
</tr>
</table>
</div>
<br />
<div id="DivCar" style="display:none">
<table width="600" border="0" cellspacing="0" cellpadding="5">
<tr>
<td>What type of car is it?</td>
<td>
<form>
<input type="radio" name="IpCar" value="Car_Sedan"> Sedan<br>
<input type="radio" name="IpCar" value="Car_Truck"> Truck<br>
</form>
</td>
</tr>
</table>
</div>
<br />
<div id="DivBike" style="display:none">
<table width="600" border="0" cellspacing="0" cellpadding="5">
<tr>
<td>What type of bike is it?</td>
<td>
<form>
<input type="radio" name="IpBike" value="Bike_Sports"> Sports<br>
<input type="radio" name="IpBike" value="Bike_Chopper"> Chopper<br>
</form>
</td>
</tr>
</table>
</div>
<br />
最佳答案
您的显示/隐藏方法没问题,但设置/删除 disabled
属性会出现问题。
您正尝试通过 ID
(IpBike、IpCar)选择 HTML 元素,但该元素已不存在。您有一个名为 IpBike、IpCar
的 radio 输入字段。
因此,您可以使用 querySelectorAll()
代替 getElementById
示例代码:
function ShowCar()
{
document.getElementById('DivCar').style.display = "";
Array.from(document.querySelectorAll('input[name="IpCar"]')).forEach(i => i.removeAttribute('disabled'));
document.getElementById('DivBike').style.display = "none";
Array.from(document.querySelectorAll('input[name="IpBike"]')).forEach(i => i.setAttribute('disabled', 'disabled'));
}
function ShowBike()
{
document.getElementById('DivBike').style.display = "";
Array.from(document.querySelectorAll('input[name="IpBike"]')).forEach(i => i.removeAttribute('disabled'));
document.getElementById('DivCar').style.display = "none";
Array.from(document.querySelectorAll('input[name="IpCar"]')).forEach(i => i.setAttribute('disabled', 'disabled'));
}
<body>
<div id="prod">
<table width="600" border="0" cellspacing="0" cellpadding="5">
<tr>
<td>Car or Bike?</td>
<td><form>
<input type="radio" name="product" id="Car" value="Car" onChange="ShowCar()" /> Car<br>
<input type="radio" name="product" id="Bike" value="Bike" onChange="ShowBike()" /> Bike<br>
</form></td>
</tr>
</table>
</div>
<br />
<div id="DivCar" style="display:none">
<table width="600" border="0" cellspacing="0" cellpadding="5">
<tr>
<td>What type of car is it?</td>
<td><form>
<input type="radio" name="IpCar" value="Car_Sedan"> Sedan<br>
<input type="radio" name="IpCar" value="Car_Truck"> Truck<br>
</form></td>
</tr>
</table>
</div>
<br />
<div id="DivBike" style="display:none">
<table width="600" border="0" cellspacing="0" cellpadding="5">
<tr>
<td>What type of bike is it?</td>
<td><form>
<input type="radio" name="IpBike" value="Bike_Sports"> Sports<br>
<input type="radio" name="IpBike" value="Bike_Chopper"> Chopper<br>
</form></td>
</tr>
</table>
</div>
关于javascript - 单击单选按钮时 div 不隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57804136/