我正在创建一些单选按钮,当我点击其中一个时,会出现一些字段。我的问题是,当我点击下一个按钮时,前一个按钮的字段会停留在屏幕上,而我需要它们消失。这是我写的代码,我知道我失败的原因是它从未在我的javascript函数中输入else语句。有人可以帮助我,我是这两种语言的新手,有没有办法保持先前的值发送到函数,以便我可以与else语句进行比较? 任何其他解决方案也会受到欢迎!
<HTML>
<HEAD> <title> Ticket Choice </title></HEAD>
<BODY>
<FORM>
<SCRIPT type="text/javascript">
function IsCheck(but_Check, if_Check){
if (document.getElementById(but_Check).checked) {
document.getElementById(if_Check).style.display = 'block';
}
else document.getElementById(if_Check).style.display = 'none';
}
</SCRIPT>
<input type = "radio" onclick = "javascript:IsCheck("buttonCheck1","ifChecked1");" name = "plane" id = "buttonCheck1"> PLANE <br>
<div id = "ifChecked1" style="display:none">
<label for = "depart">Departure:</label>
<input type = "text" id = "depart" name = "depart_plane" maxlength = "200"><br>
<label for = "dest">Destination:</label>
<input type = "text" id = "dest" name = "destination_plane" maxlength = "200"><br>
</div>
<input type = "radio" onclick = "javascript:IsCheck("buttonCheck2","ifChecked2");" name = "plane" id = "buttonCheck2"> SHIP <br>
<div id = "ifChecked2" style="display:none">
<label for = "depart">Departure:</label>
<input type = "text" id = "depart" name = "depart_ship" maxlength = "200"><br>
<label for = "dest">Destination:</label>
<input type = "text" id = "dest" name = "destination_ship" maxlength = "200"><br>
</div>
<input type = "radio" onclick = "javascript:IsCheck("buttonCheck3","ifChecked3");" name = "plane" id = "buttonCheck3"> O.S.E <br>
<div id = "ifChecked3" style="display:none">
<label for = "depart">Departure:</label>
<input type = "text" id = "depart" name = "depart_ose" maxlength = "200"><br>
<label for = "dest">Destination:</label>
<input type = "text" id = "dest" name = "destination_ose" maxlength = "200"><br>
</div>
</FORM>
</BODY>
</HTML>
答案 0 :(得分:1)
条件永远不会进入else语句,因为总是你点击一个收音机,它将被检查。
相反,试试这个:
<input type = "radio" onclick = "IsCheck(1);" name = "plane" id = "buttonCheck1"> PLANE <br>
<div id = "ifChecked1" style="display:none">
<label for = "depart">Departure:</label>
<input type = "text" id = "depart" name = "depart_plane" maxlength = "200"><br>
<label for = "dest">Destination:</label>
<input type = "text" id = "dest" name = "destination_plane" maxlength = "200"><br>
</div>
<input type = "radio" onclick = "IsCheck(2);" name = "plane" id = "buttonCheck2"> SHIP <br>
<div id = "ifChecked2" style="display:none">
<label for = "depart">Departure:</label>
<input type = "text" id = "depart" name = "depart_ship" maxlength = "200"><br>
<label for = "dest">Destination:</label>
<input type = "text" id = "dest" name = "destination_ship" maxlength = "200"><br>
</div>
<input type = "radio" onclick = "IsCheck(3);" name = "plane" id = "buttonCheck3"> O.S.E <br>
<div id = "ifChecked3" style="display:none">
<label for = "depart">Departure:</label>
<input type = "text" id = "depart" name = "depart_ose" maxlength = "200"><br>
<label for = "dest">Destination:</label>
<input type = "text" id = "dest" name = "destination_ose" maxlength = "200"><br>
</div>
JavaScript的:
function IsCheck(number){
//here we iterate the three divs
for(i = 1; i <= 3; i++){
//if the current number (i) is different to the checked input (number)
if (i != number) {
document.getElementById('ifChecked'+i).style.display = 'none';
}
else {
document.getElementById('ifChecked'+i).style.display = 'block';
}
}}