选择和取消选择certian单选按钮时切换元素可见性

时间:2018-08-18 18:27:38

标签: javascript html forms

我设法在选择单选按钮时显示了元素,但是当我选择组中的任何其他单选按钮时,该元素保持可见。我想念什么?

element.style.display = "none"是使用普通JavaScript隐藏元素的惯用方式吗?

function showApples() {
  const form = document.querySelector("form");
  const apples = form.elements["apples"];
  const appleCultivars = form.querySelector("fieldset:nth-of-type(2)");

  apples.addEventListener("click", showApples);

  if (apples.checked) {
    appleCultivars.style.display = "";
  } else {
    appleCultivars.style.display = "none";
  }
}
<body onLoad="showApples()">
<form>
<fieldset>
  <legend>Fruits</legend>
  <label><input type="radio" name="fruits" id="apples">Apples</label>
  <label><input type="radio" name="fruits" id="oranges">Oranges</label>
  <label><input type="radio" name="fruits" id="bananas">Bananas</label>
</fieldset>

<fieldset>
  <legend>Apples</legend>
  <label><input type="radio" name="apple" id="braeburn">Braeburn</label>
  <label><input type="radio" name="apple" id="macoun">Macoun</label>
  <label><input type="radio" name="apple" id="cortland">Cortland</label>
</fieldset>
</form>
</body>

我知道Stack Overflow上存在类似的问题和答案,但是它们似乎都不是优雅的,或者是在jQuery中。

2 个答案:

答案 0 :(得分:1)

看看以下内容:

const fruits = Array.from( document.getElementsByName('fruits'));
const fields = Array.from( document.getElementsByTagName('fieldset'));

fields[1].style.display = 'none';

fruits.forEach( fruit => {
  fruit.addEventListener('click', () => {
    if(fruit.value == 'apples') {
      fields[1].style.display = 'block';
    } else {
      fields[1].style.setProperty('display', 'none'); // or fields[1].style.display = 'none';
    }
  });
});
<form>
<fieldset>
  <legend>Fruits</legend>
  <label><input type="radio" name="fruits" value="apples">Apples</label>
  <label><input type="radio" name="fruits" value="oranges">Oranges</label>
  <label><input type="radio" name="fruits" value="bananas">Bananas</label>
</fieldset>

<fieldset>
  <legend>Apples</legend>
  <label><input type="radio" name="apple" value="braeburn">Braeburn</label>
  <label><input type="radio" name="apple" value="macoun">Macoun</label>
  <label><input type="radio" name="apple" value="cortland">Cortland</label>
</fieldset>
</form>

答案 1 :(得分:1)

先全部隐藏

这段代码是通用的

// uncomment the window load eventListener in your own page
//    window.addEventListener("load", function(e) { 
  document.getElementById("fruits").addEventListener("click", function(e) {
    var fruits = this.querySelectorAll("[name=fruits]");
    for (var i = 0; i < fruits.length; i++) {
      document.getElementById(fruits[i].value).classList.remove("show");
    }
    document.getElementById(e.target.value).classList.toggle("show", this.checked);

  });
//    });
.fruit {
  display: none
}

.show {
  display: block
}
<form>
  <fieldset id="fruits">
    <legend>Fruits</legend>
    <label><input type="radio" name="fruits" value="apples">Apples</label>
    <label><input type="radio" name="fruits" value="oranges">Oranges</label>
    <label><input type="radio" name="fruits" value="bananas">Bananas</label>
  </fieldset>

  <fieldset id="apples" class="fruit">
    <legend>Apples</legend>
    <label><input type="radio" name="apple" value="braeburn">Braeburn</label>
    <label><input type="radio" name="apple" value="macoun">Macoun</label>
    <label><input type="radio" name="apple" value="cortland">Cortland</label>
  </fieldset>
  <fieldset id="oranges" class="fruit">
    <legend>Oranges</legend>
    <label><input type="radio" name="orange" value="Common">Common</label>
    <label><input type="radio" name="orange" value="Blood">Blood orange</label>
    <label><input type="radio" name="orange" value="navel">Navel</label>
  </fieldset>
  <fieldset id="bananas" class="fruit">
    <legend>Bananas</legend>
    <label><input type="radio" name="banana" value="Cavendish">Cavendish</label>
    <label><input type="radio" name="banana" value="Plantain">Plantain</label>
  </fieldset>
</form>