代码之家  ›  专栏  ›  技术社区  ›  Tzar

选中和取消选中“证书”单选按钮时切换元素可见性[复制]

  •  0
  • Tzar  · 技术社区  · 7 年前

    element.style.display = "none"

    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>

    我知道在堆栈溢出上有一些类似的问题和答案,但是没有一个看起来很优雅,或者在jQuery中。

    2 回复  |  直到 7 年前
        1
  •  1
  •   SuperDJ Franklin Rivero    7 年前

    请看以下内容:

    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>
        2
  •  1
  •   mplungjan    7 年前

    先把它们都藏起来

    // 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>
    推荐文章