Home  >  Article  >  Web Front-end  >  JavaScript+bootstrap+html implements code examples of full-layer selection and multi-selection functions in hierarchical multi-selection boxes

JavaScript+bootstrap+html implements code examples of full-layer selection and multi-selection functions in hierarchical multi-selection boxes

黄舟
黄舟Original
2017-03-21 14:43:551510browse

If I want to make a function that can be sorted hierarchically and allow multiple selections, I tend to use multi-layer tags first, and directly select and add them to the text field. Next, I will introduce html+javascript## through this article. #+bootstrap Implement the full-layer and multi-select functions of the hierarchical multi-select box. Friends who need it can refer to

If I want to make a function that can be sorted by hierarchical level and can be multi-selected, the first preference is For using multi-layered tags, directly select and add it to the text field. I found this article, which is very detailed. If you need to do the functions mentioned above,

However, considering that you want to make an option The number is uncertain and may be very large. Using labels will easily cause visual fatigue. At the same time, as the number of options changes, it is required to minimize the impact on the layout and effect of the web page, so I chose to use hierarchical multi-select boxes, which is a common effect when setting up a computer. . The second layer options can be folded/expanded below the first layer, using bootstrap's

data-toggle="collapse" data-target="#demo1" to associate the second layerCollapse effect.

The specific code is as follows:

<script>
//当第一层选项选定,则该选项下的第二层全部被选定
  function allSelect(check_v, checkname) {
    var v_item = document.getElementsByName(check_v);
    var items = document.getElementsByName(checkname);
    for (var i = 0; i < items.length; ++i) {
      if (v_item[0].checked) {
        items[i].checked = true;
      }
      else {
        items[i].checked = false;
      }
    }
  }
//当第二层选项全部被选定,则第一层被选定;若第二层选项至少有一个没被选定,则第一层不被选定
  function singleSelect2parent(check_v, checkname) {
    var v_item = document.getElementsByName(check_v);
    var items = document.getElementsByName(checkname);
    var childStatus = true;
    for (var i = 0; i < items.length; ++i) {
      childStatus = (childStatus && items[i].checked);
    }
    if (childStatus) {
      v_item[0].checked = true;
    }
    else {
      v_item[0].checked = false;
    }
  }
//全选按钮,点击全选,则所有选项被选中
  function allChecked() {
    var inputItems = document.getElementsByClassName("checkbox2check");
    for (var i = 0; i < inputItems.length; i++) {
      var checkItems = document.getElementsByName("checkbox" + (i+1));
      for (var j = 0; j < checkItems.length; j++) {
        checkItems[j].checked = true;
      }
      inputItems[i].checked = true;
    }
  }
</script>
<style>
  ul li{
    list-style:none;
  }
</style>
<p>
  <input class="checkbox2check" type="checkbox" checked name="checkbox_v1" value="version1" onclick="allSelect(&#39;checkbox_v1&#39;, &#39;checkbox1&#39;)">
  <a data-toggle="collapse" data-target="#demo1">版本一<b class="caret"></b></a>
  <ul id="demo1" class="collapse in">
    <li> <input type="checkbox" checked name="checkbox1" value="layer1" onclick="singleSelect2parent(&#39;checkbox_v1&#39;, &#39;checkbox1&#39;)">v1.0.1</li>
    <li> <input type="checkbox" checked name="checkbox1" value="layer2" onclick="singleSelect2parent(&#39;checkbox_v1&#39;, &#39;checkbox1&#39;)">V1.1.1</li>
    <li> <input type="checkbox" checked name="checkbox1" value="layer3" onclick="singleSelect2parent(&#39;checkbox_v1&#39;, &#39;checkbox1&#39;)">V1.2.1</li>
    <li> <input type="checkbox" checked name="checkbox1" value="layer4" onclick="singleSelect2parent(&#39;checkbox_v1&#39;, &#39;checkbox1&#39;)">V1.3.1</li>
  </ul>
</p>
<p>
  <input class="checkbox2check" type="checkbox" checked name="checkbox_v2" value="version2" onclick="allSelect(&#39;checkbox_v2&#39;, &#39;checkbox2&#39;)">
  <a data-toggle="collapse" data-target="#demo2">版本二<b class="caret"></b></a>
  <ul id="demo2" class="collapse in">
    <li> <input type="checkbox" checked name="checkbox2" value="layer5" onclick="singleSelect2parent(&#39;checkbox_v2&#39;, &#39;checkbox2&#39;)">V2.0.1</li>
    <li> <input type="checkbox" checked name="checkbox2" value="layer6" onclick="singleSelect2parent(&#39;checkbox_v2&#39;, &#39;checkbox2&#39;)">V2.1.1</li>
    <li> <input type="checkbox" checked name="checkbox2" value="layer7" onclick="singleSelect2parent(&#39;checkbox_v2&#39;, &#39;checkbox2&#39;)">V2.2.1</li>
  </ul>
</p>
<p>
  <input class="checkbox2check" type="checkbox" checked name="checkbox_v3" value="version3" onclick="allSelect(&#39;checkbox_v3&#39;, &#39;checkbox3&#39;)">
  <a data-toggle="collapse" data-target="#demo3">版本三<b class="caret"></b></a>
  <ul id="demo3" class="collapse in">
    <li> <input type="checkbox" checked name="checkbox3" value="layer8" onclick="singleSelect2parent(&#39;checkbox_v3&#39;, &#39;checkbox3&#39;)">V3.0.1</li>
    <li> <input type="checkbox" checked name="checkbox3" value="layer9" onclick="singleSelect2parent(&#39;checkbox_v3&#39;, &#39;checkbox3&#39;)">V3.1.1</li>
    <li> <input type="checkbox" checked name="checkbox3" value="layer10" onclick="singleSelect2parent(&#39;checkbox_v3&#39;, &#39;checkbox3&#39;)">V3.2.1</li>
  </ul>
</p>
<button type="button" onclick="allChecked()">全选</button>

The above is the detailed content of JavaScript+bootstrap+html implements code examples of full-layer selection and multi-selection functions in hierarchical multi-selection boxes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn