简体   繁体   English

使用单选按钮显示div

[英]Display a div using a radio buttons

To reduce the number of lines of code I simplified my code to display three divs instead. 为了减少代码行数,我将代码简化为显示三个div。 What I want is to have a particular div displayed while hiding the other two divs depending on which radio button is checked. 我想要的是显示一个特定的div,同时隐藏其他两个div,具体取决于选中的单选按钮。

I'm actually following these two links 我实际上正在关注这两个链接

Adding event listeners to multiple elements 将事件侦听器添加到多个元素

How can I check whether a radio button is selected with JavaScript? 如何检查JavaScript是否选择了单选按钮?

in the second link it was suggested to use 在第二个链接中,建议使用

radios[i].type === 'radio' && radios[i].checked

for the test. 进行测试。

however I modified it to just 但是我将其修改为

DisplayOption[i].checked

since its all I think i need. 因为所有我想我需要的。 The error I'm receiving is 我收到的错误是

Uncaught TypeError: Cannot read property 'checked' of undefined 未捕获的TypeError:无法读取未定义的属性“选中”

 const column = document.querySelector('.column'); const table = document.querySelector('.table'); const details = document.querySelector('.details'); onload = function() { const DisplayOption = document.getElementsByClassName("DisplayOption"); for (i = 0; i < DisplayOption.length; i++) { DisplayOption[i].addEventListener('click', function() { if (DisplayOption[i].checked) { if (displyOption[i].value === "column") { column.style.display = "grid"; table.style.display = "none"; table.details.style.display = "none"; } else if (displyOption[i].value === "table") { column.style.display = "none"; table.style.display = "block"; table.details.style.display = "none"; } else { column.style.display = "none"; table.style.display = "none"; table.details.style.display = "block"; } } }, false); } } 
 .column { display: grid; } .table { display: none; } .table.details { display: none; } 
 <input type="radio" name="DisplayOption" value="column" class="DisplayOption" checked> <input type="radio" name="DisplayOption" value="table" class="DisplayOption"> <input type="radio" name="DisplayOption" value="details" class="DisplayOption"> <div class="column"> The first div is being displayed </div> <div class="table"> The second div is being displayed</div> <div class="table details"> The third div is being displayed </div> 

Inside the eventListener , to refer to the element being clicked use this instead of DisplayOption[i] . eventListener内部,要引用被单击的元素,请使用this而不是DisplayOption[i] Another bug was using table.details - it should be only details . 另一个错误是使用table.details应该只是details See corrected demo below: 请参阅下面的更正演示:

 const column = document.querySelector('.column'); const table = document.querySelector('.table'); const details = document.querySelector('.details'); onload = function() { const DisplayOption = document.getElementsByClassName("DisplayOption"); for (i = 0; i < DisplayOption.length; i++) { DisplayOption[i].addEventListener('click', function() { if (this.checked) { if (this.value === "column") { column.style.display = "grid"; table.style.display = "none"; details.style.display = "none"; } else if (this.value === "table") { column.style.display = "none"; table.style.display = "block"; details.style.display = "none"; } else { column.style.display = "none"; table.style.display = "none"; details.style.display = "block"; } } }, false); } } 
 .column { display: grid; } .table { display: none; } .table.details { display: none; } 
 <input type="radio" name="DisplayOption" value="column" class="DisplayOption" checked> <input type="radio" name="DisplayOption" value="table" class="DisplayOption"> <input type="radio" name="DisplayOption" value="details" class="DisplayOption"> <div class="column"> The first div is being displayed </div> <div class="table"> The second div is being displayed</div> <div class="table details"> The third div is being displayed </div> 

You could achieve the same using CSS ! 您可以使用CSS实现相同的目标! No need of javascript ! 不需要JavaScript!

You can make use of the sibling (~) selector in CSS and display the corresponding div 您可以在CSS中使用sibling (〜)选择器并显示相应的div

 div { display: none; } input[value="column"]:checked~.column, input[value="table"]:checked~.table, input[value="details"]:checked~.details { display: block; } 
 <input type="radio" name="DisplayOption" value="column" class="DisplayOption" checked> <input type="radio" name="DisplayOption" value="table" class="DisplayOption"> <input type="radio" name="DisplayOption" value="details" class="DisplayOption"> <div class="column"> The first div is being displayed </div> <div class="table"> The second div is being displayed</div> <div class="details"> The third div is being displayed </div> 

JSFiddle link JSFiddle链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM