gpt4 book ai didi

javascript - 如何在下拉列表中创建复选框?

转载 作者:技术小花猫 更新时间:2023-10-29 12:21:59 26 4
gpt4 key购买 nike

我想创建一个多选下拉列表。实际上,我必须使用下拉菜单选择多个选项。当我按如下所示简单地执行此操作时:

    <select>
<option><input type="checkbox"></option>
</select>

然后复选框显示在下拉字段前面。但我想为每个选项创建它而不是为整个选项创建它,以便我可以选择多个选项。有什么办法吗?

最佳答案

这是一个简单的下拉 list :

var checkList = document.getElementById('list1');
checkList.getElementsByClassName('anchor')[0].onclick = function(evt) {
if (checkList.classList.contains('visible'))
checkList.classList.remove('visible');
else
checkList.classList.add('visible');
}
.dropdown-check-list {
display: inline-block;
}

.dropdown-check-list .anchor {
position: relative;
cursor: pointer;
display: inline-block;
padding: 5px 50px 5px 10px;
border: 1px solid #ccc;
}

.dropdown-check-list .anchor:after {
position: absolute;
content: "";
border-left: 2px solid black;
border-top: 2px solid black;
padding: 5px;
right: 10px;
top: 20%;
-moz-transform: rotate(-135deg);
-ms-transform: rotate(-135deg);
-o-transform: rotate(-135deg);
-webkit-transform: rotate(-135deg);
transform: rotate(-135deg);
}

.dropdown-check-list .anchor:active:after {
right: 8px;
top: 21%;
}

.dropdown-check-list ul.items {
padding: 2px;
display: none;
margin: 0;
border: 1px solid #ccc;
border-top: none;
}

.dropdown-check-list ul.items li {
list-style: none;
}

.dropdown-check-list.visible .anchor {
color: #0094ff;
}

.dropdown-check-list.visible .items {
display: block;
}
<div id="list1" class="dropdown-check-list" tabindex="100">
<span class="anchor">Select Fruits</span>
<ul class="items">
<li><input type="checkbox" />Apple </li>
<li><input type="checkbox" />Orange</li>
<li><input type="checkbox" />Grapes </li>
<li><input type="checkbox" />Berry </li>
<li><input type="checkbox" />Mango </li>
<li><input type="checkbox" />Banana </li>
<li><input type="checkbox" />Tomato</li>
</ul>
</div>

关于javascript - 如何在下拉列表中创建复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19206919/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com