这篇文章主要为大家展示了“html+javascript+bootstrap如何实现层级多选框全层全选和多选功能”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“html+javascript+bootstrap如何实现层级多选框全层全选和多选功能”这篇文章吧。
具体代码如下:
<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>
<div>
<input class="checkbox2check" type="checkbox" checked name="checkbox_v1" value="version1" onclick="allSelect('checkbox_v1', 'checkbox1')"><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('checkbox_v1', 'checkbox1')">v1.0.1</li>
<li> <input type="checkbox" checked name="checkbox1" value="layer2" onclick="singleSelect2parent('checkbox_v1', 'checkbox1')">V1.1.1</li>
<li> <input type="checkbox" checked name="checkbox1" value="layer3" onclick="singleSelect2parent('checkbox_v1', 'checkbox1')">V1.2.1</li>
<li> <input type="checkbox" checked name="checkbox1" value="layer4" onclick="singleSelect2parent('checkbox_v1', 'checkbox1')">V1.3.1</li>
</ul>
</div>
<div>
<input class="checkbox2check" type="checkbox" checked name="checkbox_v2" value="version2" onclick="allSelect('checkbox_v2', 'checkbox2')"><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('checkbox_v2', 'checkbox2')">V2.0.1</li>
<li> <input type="checkbox" checked name="checkbox2" value="layer6" onclick="singleSelect2parent('checkbox_v2', 'checkbox2')">V2.1.1</li>
<li> <input type="checkbox" checked name="checkbox2" value="layer7" onclick="singleSelect2parent('checkbox_v2', 'checkbox2')">V2.2.1</li>
</ul>
</div>
<div>
<input class="checkbox2check" type="checkbox" checked name="checkbox_v3" value="version3" onclick="allSelect('checkbox_v3', 'checkbox3')"><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('checkbox_v3', 'checkbox3')">V3.0.1</li>
<li> <input type="checkbox" checked name="checkbox3" value="layer9" onclick="singleSelect2parent('checkbox_v3', 'checkbox3')">V3.1.1</li>
<li> <input type="checkbox" checked name="checkbox3" value="layer10" onclick="singleSelect2parent('checkbox_v3', 'checkbox3')">V3.2.1</li>
</ul>
</div>
<button type="button" onclick="allChecked()">全选</button>
以上是“html+javascript+bootstrap如何实现层级多选框全层全选和多选功能”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网行业资讯频道!