简体   繁体   English

使用涉及“ checkbox.checked === true”的if / then命令在涉及另一个复选框时隐藏/显示元素时出现问题

[英]Problem using if/then command with “checkbox.checked === true” to hide/show an element when there's another checkbox involved

TL;DR: How to have a checkbox that displays a certain div element if all radio buttons are blank, but if you have a certain radio button selected, it will display a different div element when you check it. TL; DR:如果所有单选按钮都为空白,如何具有显示某个div元素的复选框,但是如果您选择了某个单选按钮,则选中该复选框将显示不同的div元素。 The code I have isn't working and I don't know why. 我拥有的代码无法正常工作,我也不知道为什么。

Sorry, this is going to be a very long and complicated question. 抱歉,这将是一个非常漫长而复杂的问题。 I have a checkbox that shows or hides a class of div elements ("c") depending on whether or not the checkbox is checked, which has been working fine. 我有一个复选框可以显示或隐藏div元素类(“ c”),具体取决于是否已选中该复选框,效果一直很好。 I also have two radio buttons located elsewhere on the page, which, when selected (provided that "c" is also selected), each show different div elements that are located inside one of the "c" elements - let's call those inner divs "one" and "two". 我在页面其他位置也有两个单选按钮,当选中它们时(假设还选择了“ c”),每个按钮都显示位于“ c”元素之一内部的不同div元素-我们将这些内部div称为“一”和“二”。 Those have been working fine as well. 那些也很好。

By default, when the checkbox is checked, "one" is shown, even if neither radio button has been selected yet. 默认情况下,选中此复选框时,即使尚未选择任何一个单选按钮,也会显示“一个”。 If someone checks the checkbox and then the radio button for "two", "one" disappears and "two" takes its place. 如果有人选中该复选框,然后单选按钮为“两个”,则“一个”消失,而“两个”取而代之。 My difficulty is this: If someone checks "c" (thereby displaying "one" as well), then selects the radio button to display "two" instead of "one", unchecks "c" and then checks it again , I want "two" to reappear instead of "one". 我的困难是:如果有人选中“ c”(从而也显示“一个”),然后选择单选按钮以显示“两个”而不是“一个”, 取消选中“ c”,然后再次选中 ,我想要“重新出现两个”而不是“一个”。 In other words, I want to still display "one" by default when "c" is checked, but I also want to make it so that if the radio button for "two" is already selected and the checkbox is unchecked , clicking the checkbox will show "two" instead of "one". 换句话说,我想在选中“ c”时仍默认显示“一个”,但我也想使其保持选中状态,以便如果已经选择了“两个”单选按钮并且未选中该复选框,请单击该复选框。将显示“两个”而不是“一个”。

For the checkbox, I just have an onclick function that hides or shows "c" (via display:none / display:block) each time the checkbox is clicked. 对于该复选框,我只有一个onclick函数,该函数在每次单击该复选框时都会隐藏或显示“ c”(通过display:none / display:block)。 The two radio buttons work the same way (except the checkbox has to be selected in order for "w" or "d" to actually show up, since they're hidden if "c" is hidden). 这两个单选按钮的工作方式相同(除了必须选中该复选框,以使“ w”或“ d”实际显示,因为如果隐藏了“ c”,则它们也会被隐藏)。

Right now, the onclick function that hides/shows "c" says that if the radio button for "two" is selected, then "two" should show up; 现在,隐藏/显示“ c”的onclick函数表示,如果选择了“两个”单选按钮,则应该显示“两个”;否则,将显示“两个”。 if not, then "one" should show up. 如果不是,则应显示“一个”。 (That's how I make "one" display by default without forcing it to appear whenever the checkbox is clicked, since neither radio button is selected when you load the page.) For some reason, it's not working, and I don't understand why. (这是我默认情况下使“一个”显示的方式,而不强制每次单击该复选框都显示它,因为加载页面时未选中任何一个单选按钮。)出于某种原因,它不起作用,我也不明白为什么。

Also, not sure if this matters, but the radio buttons are in one of the "c" elements and only appear when the checkbox is clicked. 同样,不确定这是否重要,但是单选按钮位于“ c”元素之一中,仅在单击复选框时才会显示。 Anyway, here are the relevant parts of my code: 无论如何,这是我的代码的相关部分:

HTML: HTML:

<input type="checkbox" onclick="showdiv()"> Checkbox to show all divs in the class 'data'

<div class="data" style="display:none;">
<input type="radio" id="radio1" name="a" onclick="one()"> Radio button to show first div
<input type="radio" id="radio2" name="a" onclick="two()"> Radio button to show second div
</div>

<div class="data" style="display:none;">
<div id="one" style="display:none;">first div</div>
<div id="two" style="display:none;">second div</div>
</div>

Javascript: Javascript:

<script type="text/javascript">

var data = document.getElementsByClassName("data");
var one = document.getElementById("one");
var two = document.getElementById("two");

function one() {
    one.style.display = "block";
    two.style.display = "none";
}

function two() {
    two.style.display = "block";
    one.style.display = "none";
}

function showdiv() {
    for(var i = 0; i < data.length; i++) {
        data[i].style.display === "none" ?
        data[i].style.display = "block" :
        data[i].style.display = "none";}
    var checktwo = document.getElementById("radio2");
    if (checktwo.checked === true) {
        (two.style.display = "block") &&
        (one.style.display = "none");}
    else {
        (one.style.display = "block") &&
        (two.style.display = "none");}
}

</script>

What's happening is this: I check the checkbox to display "c" and "one" appears by default (as it should, since the function showdiv() says to show "one" only if the radio button for "two" isn't currently selected). 这是怎么回事:我选中该复选框以显示“ c”,并且默认情况下会显示“一个”(因为应该这样做,因为函数showdiv()表示仅当 “两个”单选按钮未显示时才显示“一个”当前选择)。 I select the radio button to display "two" instead of "one", and that works fine. 我选择单选按钮以显示“两个”而不是“一个”,并且效果很好。 But then when I go to uncheck and re-check the box to display "c", "one" shows up when it should be showing "two". 但是,当我取消选中并再次选中显示“ c”的框时,应在显示“两个”时显示“一个”。

Hopefully it's a simple fix - I basically learned Javascript just to make this thing I'm working on and I'm still figuring out basic stuff, so maybe I just missed something easy. 希望这是一个简单的修复方法-我基本上是学习Javascript来做我正在研究的事情,而我仍在寻找基本的东西,所以也许我只是错过了一些简单的事情。 I'd be eternally grateful to anyone who can help me out with this. 我将永远感谢任何可以帮助我解决这个问题的人。

You're creating too many functions for just toggling one level deep, nested divs. 您正在创建太多的功能,仅用于切换一个级别的深层嵌套div。 Also, you should avoid using inline on* handlers (onclick, oninput, etc) and replace them with event listeners instead as follows: 另外,您应该避免使用内联on* handlers (onclick,oninput等),而应将它们替换为event listeners ,如下所示:

All you need is two functions: 您只需要两个功能:

  • one function say, showdiv() for toggling the main div when the first input is clicked and, 一个函数说, showdiv()用于在单击第一个输入时切换主div,
  • another function say, showInnerDiv() for toggling the #one and #two divs. 另一个函数showInnerDiv()用于切换#one#two div。

Check and run the Code Snippet below for a practical example of the above: 检查并运行下面的代码片段 ,以获取上述示例:

 var input = document.querySelector('input'); // retrieve first input var innerBtns = document.querySelectorAll('input[type^="radio"]'); // retrieve the radio buttons function showdiv() { var data = document.querySelectorAll(".data"); // retrieve both .data divs data.forEach(x => { //use forEach to toggle the display of each .data div x.style.display = (x.style.display === "none") ? "block" : "none"; }); document.getElementById("one").style.display = "block"; document.getElementById("two").style.display = "none"; } function showInnerDiv() { var one = document.getElementById("one"); var two = document.getElementById("two"); var checktwo = document.getElementById("radio2"); if (checktwo.checked === true) { two.style.display = "block"; one.style.display = "none"; } else { one.style.display = "block"; two.style.display = "none"; } } input.addEventListener("click", showdiv); innerBtns.forEach(btn => { //use forEach to add a click listener to each radio button btn.addEventListener("click", showInnerDiv); }) 
 #one {background-color:red; padding: 10px;margin-top: 10px;} #two {background-color:yellow; padding: 10px;margin-top: 10px;} 
 <input type="checkbox"> Checkbox to show all divs in the class 'data' <div class="data" style="display:none;"> <input type="radio" id="radio1" name="a"> Radio button - first div <input type="radio" id="radio2" name="a"> Radio button - second div </div> <div class="data" style="display:none;"> <div id="one" style="display:none;">first div</div> <div id="two" style="display:none;">second div</div> </div> 

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

相关问题 在iCheck的复选框上显示和隐藏另一个复选框处于选中和未选中状态 - Show And Hide Another Checkbox on iCheck's checkbox checked and unchecked state checkbox.checked在onclick函数中始终返回true - checkbox.checked always returning true inside onclick function 如何使用jQuery在复选框的选中/未选中状态上显示/隐藏元素 - How to show/hide an element on checkbox checked/unchecked states using jQuery 选中时使用多个复选框显示/隐藏多个 Div - Show/Hide Multiple Div using multiple checkbox when checked 选中复选框时如何使用 id 显示和隐藏 - How to Show and Hide with id when the checkbox is checked 选中复选框时显示/隐藏其余行 - Show/hide rest of the rows when checkbox is checked 选中复选框时隐藏 iframe 中的元素 - Hide element in an iframe when checkbox is checked 添加新的动态行时Checkbox.checked更改 - Checkbox.checked change when adding new dynamic row 如何在选中复选框时隐藏内容并在未选中复选框时显示? - How to hide content when checkbox is checked and show when checkbox is unchecked? 当另一列为真/假时,数据表编辑器显示/隐藏编辑行中复选框列表的复选框 - Datatables Editor show/hide a checkbox of a list of checkbox in edit row when another column is true/false
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM