如果我按下'x'按钮,它将取消选中所有部分并关闭它?

时间:2017-06-19 03:56:43

标签: html checkbox

如果我按下“x”按钮,它将取消选中所有部分并将其关闭?根据我的编码,我只是设法做到顶部,但在底部它不是取消选中,底部的复选框没有关闭,我希望它取消选中并关闭底部复选框。

“解决”复选框中的示例。当我勾选时,会有另一个复选框。当我按下“x”按钮时,它将取消选中“解决”复选框。如何在“解决”复选框底部取消选中所有内容,并在按下“x”按钮时将其关闭。

<header>
  <h3><b><label for="search">Search</label></b>
    <input id="search" type="text" name="search" placeholder="Search.." onkeyup="filterWithSearch(event)" /></h3>

  <script>
    function fungsi() {

      var a = document.getElementById("subSolving1").checked;
      var b = document.getElementById("subSolving2").checked;
      var c = document.getElementById("subSolving3").checked;

      var d = document.getElementById("subConclusion1").checked;
      var e = document.getElementById("subConclusion2").checked;

      var f = document.getElementById("subRepeated1").checked;
      var g = document.getElementById("subRepeated2").checked;

      console.log(a, b, c, d, e, f, g);

      if (c || d === true) {
        document.getElementById("Status").innerHTML = "Problem1";
      } else if (b || a || e) {
        document.getElementById("Status").innerHTML = "Problem2";
      } else if (f && g) {
        document.getElementById("Status").innerHTML = "Problem5";
      } else if (f) {
        document.getElementById("Status").innerHTML = "Problem3";
      } else if (g) {
        document.getElementById("Status").innerHTML = "Problem4";
      }
    }

    function filterWithSearch(e) {
      console.log(e)
      if (e.which === 13) {
        var searchStr = e.target.value.toLowerCase();
        var cxb = document.getElementById('subOptionsContainer').querySelectorAll("label")

        var subSolving = document.getElementById("subSolving");
        var subConclusion = document.getElementById("subConclusion");
        var subRepeated = document.getElementById("subRepeated");

        if (searchStr === '') {
          subSolving.style.display = Solving.checked ? "block" : "none";
          subConclusion.style.display = Conclusion.checked ? "block" : "none";
          subRepeated.style.display = Repeated.checked ? "block" : "none";
        } else {
          subSolving.style.display = "block";
          subConclusion.style.display = "block";
          subRepeated.style.display = "block";
        }

        for (var i = 0; i < cxb.length; i++) {
          //console.log(cxb[i])
          if (cxb[i].textContent.toLowerCase().indexOf(searchStr) !== -1) {
            cxb[i].style.display = 'block';
          } else {
            cxb[i].style.display = 'none';
          }

        }
      }
  </script>

  <body>

    <b>Original:</b>
    <div id="Alltry">
      <script type="text/javascript">
        function ShowHideDiv(Solving) {
          var subSolving = document.getElementById("subSolving");
          subSolving.style.display = Solving.checked ? "block" : "none";

          var cxb = subSolving.getElementsByTagName('label');
          for (var i = 0; i < cxb.length; i++) {
            //console.log(cxb[i])
            if (Solving.checked) {
              cxb[i].style.display = 'block';
            } else {
              cxb[i].style.display = 'none';
            }
          }
        }

        function uncheckAll(divid) {
          var checks = document.querySelectorAll('#' + divid + ' input[type="checkbox"]');
          for (var i = 0; i < checks.length; i++) {
            var check = checks[i];
            if (!check.disabled) {
              check.checked = false;
            }
          }
        }
      </script>
      <div style="float: left; max-width: 80%;"><label for="Solving"> <input type="checkbox" id="Solving" onclick="ShowHideDiv(this)" value="Solving"/>Solving</label>
      </div>
    </div>

    <script type="text/javascript">
      function ShowHideDiv2(Conclusion) {
        var subConclusion = document.getElementById("subConclusion");
        subConclusion.style.display = Conclusion.checked ? "block" : "none";
      }
    </script>
    <label for="Conclusion"> <input type="checkbox" id="Conclusion" onclick="ShowHideDiv2(this)" value="Conclusion"/>Conclusion</label>

    <script type="text/javascript">
      function ShowHideDiv3(Repeated) {
        var subRepeated = document.getElementById("subRepeated");
        subRepeated.style.display = Repeated.checked ? "block" : "none";
      }
    </script>
    <label for="Repeated"> <input type="checkbox" id="Repeated" onclick="ShowHideDiv3(this)" value="Repeated"/>
			Repeated</label>
    <div id="subOptionsContainer">


      <div id="subSolving" style="display: none">

        <input type="button" id="uncheckAll" onclick="uncheckAll('Alltry')" value="x"><br>

        <p><label> <input type="checkbox" id="subSolving1" onclick="fungsi()"/>subSolving1 
					</label></p>

        <p><label> <input type="checkbox" id="subSolving2" onclick="fungsi()"/>subSolving2</label></p>

        <p><label> <input type="checkbox" id="subSolving3" onclick="fungsi()"/>subSolving3 </label></p>
      </div>

      <div id="subConclusion" style="display: none">

        <p><label> <input type="checkbox" id="subConclusion1" onclick="fungsi()"/>subConclusion</label></p>

        <p><label> <input type="checkbox" id="subConclusion2" onclick="fungsi()"/>subConclusion
					</label></p>
      </div>

      <div id="subRepeated" style="display: none">
        <p><label> <input type="checkbox" id="subRepeated1" onclick="fungsi()"/>subRepeated1</label></p>

        <p><label> <input type="checkbox" id="subRepeated2" onclick="fungsi()"/>subRepeated2
					</label></p>
      </div>
    </div>
    <b><hr/><p id="Status"></p></b></header>

</div>
</div>

1 个答案:

答案 0 :(得分:2)

只需将此功能修改为uncheckAll(divid),如下所示

 function uncheckAll(divid) {
              var checks = document.querySelectorAll('#' + divid + ' input[type="checkbox"]');

              for (var i = 0; i < checks.length; i++) {
                var check = checks[i];
                if (!check.disabled) {
                  check.checked = false;
                  ShowHideDiv(Solving);
              ShowHideDiv(Solving);
              ShowHideDiv2(this);
              ShowHideDiv3(this);
              var Conclusion = document.getElementById("Conclusion").checked = false;
 document.getElementById("Repeated").checked = false;
                }
              }
            }

<header>
  <h3><b><label for="search">Search</label></b>
    <input id="search" type="text" name="search" placeholder="Search.." onkeyup="filterWithSearch(event)" /></h3>

  <script>
    function fungsi() {

      var a = document.getElementById("subSolving1").checked;
      var b = document.getElementById("subSolving2").checked;
      var c = document.getElementById("subSolving3").checked;

      var d = document.getElementById("subConclusion1").checked;
      var e = document.getElementById("subConclusion2").checked;

      var f = document.getElementById("subRepeated1").checked;
      var g = document.getElementById("subRepeated2").checked;

      console.log(a, b, c, d, e, f, g);

      if (c || d === true) {
        document.getElementById("Status").innerHTML = "Problem1";
      } else if (b || a || e) {
        document.getElementById("Status").innerHTML = "Problem2";
      } else if (f && g) {
        document.getElementById("Status").innerHTML = "Problem5";
      } else if (f) {
        document.getElementById("Status").innerHTML = "Problem3";
      } else if (g) {
        document.getElementById("Status").innerHTML = "Problem4";
      }
    }

    function filterWithSearch(e) {
      console.log(e)
      if (e.which === 13) {
        var searchStr = e.target.value.toLowerCase();
        var cxb = document.getElementById('subOptionsContainer').querySelectorAll("label")

        var subSolving = document.getElementById("subSolving");
        var subConclusion = document.getElementById("subConclusion");
        var subRepeated = document.getElementById("subRepeated");

        if (searchStr === '') {
          subSolving.style.display = Solving.checked ? "block" : "none";
          subConclusion.style.display = Conclusion.checked ? "block" : "none";
          subRepeated.style.display = Repeated.checked ? "block" : "none";
        } else {
          subSolving.style.display = "block";
          subConclusion.style.display = "block";
          subRepeated.style.display = "block";
        }

        for (var i = 0; i < cxb.length; i++) {
          //console.log(cxb[i])
          if (cxb[i].textContent.toLowerCase().indexOf(searchStr) !== -1) {
            cxb[i].style.display = 'block';
          } else {
            cxb[i].style.display = 'none';
          }

        }
      }
  </script>

  <body>

    <b>Original:</b>
    <div id="Alltry">
      <script type="text/javascript">
        function ShowHideDiv(Solving) {
          var subSolving = document.getElementById("subSolving");
          subSolving.style.display = Solving.checked ? "block" : "none";

          var cxb = subSolving.getElementsByTagName('label');
          for (var i = 0; i < cxb.length; i++) {
            //console.log(cxb[i])
            if (Solving.checked) {
              cxb[i].style.display = 'block';
            } else {
              cxb[i].style.display = 'none';
            }
          }
        }

        function uncheckAll(divid) {
          var checks = document.querySelectorAll('#' + divid + ' input[type="checkbox"]');
          
          for (var i = 0; i < checks.length; i++) {
            var check = checks[i];
            if (!check.disabled) {
              check.checked = false;
              ShowHideDiv(Solving);
              ShowHideDiv2(this);
              ShowHideDiv3(this);
  document.getElementById("Conclusion").checked = false;
  document.getElementById("Repeated").checked = false;
  document.getElementById("subSolving1").checked = false;
  document.getElementById("subSolving2").checked = false;
  document.getElementById("subSolving3").checked = false;
  document.getElementById("subConclusion1").checked = false;
  document.getElementById("subConclusion2").checked = false;
  document.getElementById("subRepeated1").checked = false;
  document.getElementById("subRepeated2").checked = false;

            }
          }
        }
      </script>
      <div style="float: left; max-width: 80%;"><label for="Solving"> <input type="checkbox" id="Solving" onclick="ShowHideDiv(this)" value="Solving"/>Solving</label>
      </div>
    </div>

    <script type="text/javascript">
      function ShowHideDiv2(Conclusion) {
        var subConclusion = document.getElementById("subConclusion");
        subConclusion.style.display = Conclusion.checked ? "block" : "none";
      }
    </script>
    <label for="Conclusion"> <input type="checkbox" id="Conclusion" onclick="ShowHideDiv2(this)" value="Conclusion"/>Conclusion</label>

    <script type="text/javascript">
      function ShowHideDiv3(Repeated) {
        var subRepeated = document.getElementById("subRepeated");
        subRepeated.style.display = Repeated.checked ? "block" : "none";
      }
    </script>
    <label for="Repeated"> <input type="checkbox" id="Repeated" onclick="ShowHideDiv3(this)" value="Repeated"/>
			Repeated</label>
    <div id="subOptionsContainer">


      <div id="subSolving" style="display: none">

        <input type="button" id="uncheckAll" onclick="uncheckAll('Alltry')" value="x"><br>

        <p><label> <input type="checkbox" id="subSolving1" onclick="fungsi()"/>subSolving1 
					</label></p>

        <p><label> <input type="checkbox" id="subSolving2" onclick="fungsi()"/>subSolving2</label></p>

        <p><label> <input type="checkbox" id="subSolving3" onclick="fungsi()"/>subSolving3 </label></p>
      </div>

      <div id="subConclusion" style="display: none">

        <p><label> <input type="checkbox" id="subConclusion1" onclick="fungsi()"/>subConclusion</label></p>

        <p><label> <input type="checkbox" id="subConclusion2" onclick="fungsi()"/>subConclusion
					</label></p>
      </div>

      <div id="subRepeated" style="display: none">
        <p><label> <input type="checkbox" id="subRepeated1" onclick="fungsi()"/>subRepeated1</label></p>

        <p><label> <input type="checkbox" id="subRepeated2" onclick="fungsi()"/>subRepeated2
					</label></p>
      </div>
    </div>
    <b><hr/><p id="Status"></p></b></header>

</div>
</div>