这段代码已经正常运行。然而,我需要帮助使“input”标签显示为下拉框。我尝试将输入框样式设置为下拉框但并没有成功。我还在寻找一种通过动态构建选项来使用下拉框的方法。非常感谢任何和所有的帮助。
$(function () {
var availableTags = new Array(1000000);
var j = 0;
for (var i = 1; i < availableTags.length; i++) {
availableTags[j++] = i.toString();
}
$("#tags").autocomplete({
source: function (req, responseFn) {
var re = req.term;
var matcher = new RegExp("^" + re, "i");
var a = $.grep(availableTags, function (item) {
return matcher.test(item);
});
responseFn(a.slice(0, 5));
},
select: function (event, ui) {
if (ui.item && ui.item.value) {
var titleinput = ui.item.value;
ui.item.value = $.trim(titleinput);
alert(ui.item.value);
}
},
change: function (event, ui) {
if (!valid) {
// remove invalid value, as it didn't match anything
$(this).val("");
select.val("");
return false;
}
}
});
});
.custom-combobox {
position: relative;
display: inline-block;
border-style: solid;
border-width: 1px;
}
.custom-combobox-toggle {
position: absolute;
top: 0;
bottom: 0;
margin-left: -1px;
padding: 0;
}
.custom-combobox-input {
margin: 0;
padding: 5px 10px;
}
/*
.custom-combobox-list-item {
color: blue;
font-weight: bold;
}
.custom-combobox-input , .custom-combobox-list-item.ui-state-focus {
color: black;
font-weight: bold;
font-style: italic;
}
*/
#tags {
width: 40px;
}
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css">
<label for="tags">Tags: </label>
<input id="tags" type="text" class="custom-combobox" value="">