使用javascript和html使字段出现/消失
Making fields appear/ disappear using javascript and html
我正在创建一些单选按钮,当我点击其中一个字段出现。我的问题是,当我点击下一个按钮,从前一个字段留在屏幕上,而我需要他们消失。这是我写的代码,我知道我失败的原因是它从来没有在我的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>
这个条件永远不会进入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';
}
}}
相关文章:
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 而循环只设置php中输入字段中的第一个值
- 在输入字段中将最小金额设置为
- 从Rally获取一个特定的标记,以便计算另一个字段中的值
- 如何在输入字段中的按钮的帮助下打开日历,该字段的类型为“=”;日期”;
- JavaScript 检查隐藏字段中的值并将其设置为隐藏字段值
- 在wordpress一定时间后更改自定义字段
- 将phonegap图像发布到php/mysql-图像字段从发布请求中消失
- 当我在HTML中选择特定选项时,要消失的输入字段
- 隐藏/禁用字段从 req.body 中消失(Express JS bodyParser)
- 更改焦点后,输入字段中的文本将消失
- 输入字段html5占位符没有'不要在javascript中添加值后就消失
- 动态添加的输入字段立即消失
- 输入字段逐渐消失,取代了网站的其他部分
- jQuery Validator无效输入字段在成功提交后消失
- ExtJs文件字段在第一次使用后消失
- 使用Angular.js点击输入类型文件字段时,图像名称会消失
- mm菜单&prototype.js冲突,html在使用搜索字段时消失
- 使用javascript和html使字段出现/消失
- 用户单击输入字段,值将消失