使用搜索将数组中的值添加到Bootstrap组合框可以通过以下步骤实现:
以下是一个示例代码:
<!DOCTYPE html>
<html>
<head>
<title>Search and Add Values to Bootstrap Combobox</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h2>Search and Add Values to Bootstrap Combobox</h2>
<div class="form-group">
<input type="text" id="searchInput" class="form-control" placeholder="Search...">
<select id="combobox" class="form-control"></select>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script>
var array = ["Apple", "Banana", "Orange", "Mango", "Grapes"];
// 绑定键盘输入事件
$("#searchInput").on("input", function() {
var searchValue = $(this).val();
var matches = [];
// 搜索匹配项
for (var i = 0; i < array.length; i++) {
if (array[i].toLowerCase().indexOf(searchValue.toLowerCase()) !== -1) {
matches.push(array[i]);
}
}
// 清空下拉列表框
$("#combobox").empty();
// 添加匹配项到下拉列表框
for (var j = 0; j < matches.length; j++) {
$("#combobox").append("<option>" + matches[j] + "</option>");
}
});
// 绑定点击事件
$("#combobox").on("click", "option", function() {
var selectedValue = $(this).text();
$("#searchInput").val(selectedValue);
});
</script>
</body>
</html>
这个示例代码实现了一个简单的搜索功能,可以将数组中匹配到的值添加到Bootstrap组合框中。你可以根据实际需求进行修改和扩展。
领取专属 10元无门槛券
手把手带您无忧上云