目前,我的数据库中有 200 多个国家/地区列表。我想为国家/地区选择创建一个复选框。即使我已经将 html 类型设置为复选框,该复选框在我的选择表单中仍然不存在。实际上如何创建复选框?
HTML:
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Country</label>
<select class="form-control" name="country_id[]" multiple size="10" style="height: 100%;">
@foreach ($countries as $item)
<option value="{{$item->id}}" selected>{{$item->name}}</option>
@endforeach
</select>
</div>
</div>
</div>
最佳答案
您不能将复选框放置在 select 元素内,但您可以通过使用 HTML、CSS 和 JavaScript 获得相同的功能。
var expanded = false;
function showCheckboxes() {
var checkboxes = document.getElementById("checkboxes");
if (!expanded) {
checkboxes.style.display = "block";
expanded = true;
} else {
checkboxes.style.display = "none";
expanded = false;
}
}
.multiselect {
width: 200px;
}
.selectBox {
position: relative;
}
.selectBox select {
width: 100%;
font-weight: bold;
}
.overSelect {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
#checkboxes {
display: none;
border: 1px #dadada solid;
}
#checkboxes label {
display: block;
}
#checkboxes label:hover {
background-color: #1e90ff;
}
<form>
<div class="multiselect">
<div class="selectBox" onclick="showCheckboxes()">
<select>
<option>Select an option</option>
</select>
<div class="overSelect"></div>
</div>
<div id="checkboxes">
<label for="one">
<input type="checkbox" id="one" />First checkbox</label>
<label for="two">
<input type="checkbox" id="two" />Second checkbox</label>
<label for="three">
<input type="checkbox" id="three" />Third checkbox</label>
</div>
</div>
</form>
您也可以使用Bootstrap Multiselect插件。
关于javascript - 如何在表单组选择 HTML Javascript 中创建复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60296144/