[英]How to display an element when radio button is selected and hide it when is not selected using javascript?
我想制作一个互动的在线表格。 当人们选择第一个问题的一个选项时,相应的元素将在选中时显示,而在选择另一个选项时则隐藏。 (选择其他按钮时,应显示其他元素)
这是我到目前为止的代码。
function showStuff(id, btn) { if (document.getElementById(id).style.display == "none") { document.getElementById(id).style.display = 'block'; } else if (document.getElementById(id).style.display == 'block') { document.getElementById(id).style.display = 'none'; } }
<input name="q1" type="radio" value="A" onchange="showStuff('A', this); return false;" /> <input name="q1" type="radio" value="B" onchange="showStuff('B', this); return false;" /> <div id="A" style="display:none"> This should display only when A button is clicked </div> <div id="B" style="display:none"> This should display only when B button is clicked </div>
一旦显示它,选择其他选项就无法隐藏它。 请问您有什么好的建议吗? 非常感谢!
您就快到了。只需添加一行代码即可在每次点击时隐藏两个div。
使用jQuery:
$("#A,#B").hide();
没有jQuery:
document.getElementById("A").style.display = 'none'; document.getElementById("B").style.display = 'none';
请参阅下面的完整工作代码:
function showStuff(id, btn) { $("#A,#B").hide(); // hide both each time document.getElementById(id).style.display = 'block'; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input name="q1" type="radio" value="A" onchange="showStuff('A', this); return false;" /> <input name="q1" type="radio" value="B" onchange="showStuff('B', this); return false;" /> <div id="A" style="display:none"> This should display only when A button is clicked </div> <div id="B" style="display:none"> This should display only when B button is clicked </div>
尝试这个:
<script type="text/javascript">
function showStuff(id, btn) {
document.getElementById("A").style.display = 'none';
document.getElementById("B").style.display = 'none';
if (id == "A") {
document.getElementById("A").style.display = 'block';
} else {
document.getElementById("B").style.display = 'block';
}
}
</script>
我会推荐这个功能
function showStuff(id, btn) {
document.getElementById('A').style.display = 'none';
document.getElementById('B').style.display = 'none';
document.getElementById(id).style.display = 'block';
}
如果您要添加另一个选项,别忘了在函数的开头隐藏所有可用选项。
使用jQuery,事情可能会容易得多-您可以使用选择器$('input [name = q1]')选择所有输入,而只用一行隐藏所有选项。
$('input[name=q1]').hide();
function showStuff(value,btn) { if(value=='A'){ document.getElementById("A").style.display = 'block'; document.getElementById("B").style.display = 'none'; } else if(value=='B'){ document.getElementById("B").style.display = 'block'; document.getElementById("A").style.display = 'none'; } }
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="style.css"> <script src="script.js"></script> </head> <body> <input name="q1" type="radio" value="A" onchange="showStuff('A', this); return false;" /> <input name="q1" type="radio" value="B" onchange="showStuff('B', this); return false;"/> <div id="A" style="display:none"> This should display only when A button is clicked </div> <div id="B" style="display:none"> This should display only when B button is clicked </div> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.