javascript - 有没有一种简单的方法来使用自动完成搜索来触发预定义的模态?
问题描述
我是一个试图拼凑现有资源的非开发人员,所以我完全有可能遗漏了那里的东西——也就是说,我已经进行了大量的搜索和阅读,但还没有找到我正在寻找的东西暂时。
[扩展背景和问题] 我正在开展一个项目,其中合作者对完成的站点有明确的愿景 - 一个搜索栏,其中包含有关数据集的预加载问题,该数据集以模式返回问题的答案。例如,如果您开始输入“多少……”,您会在搜索栏中看到诸如“2015 年售出多少小部件?”、“书架上有多少本书?”之类的下拉菜单。等(显然不是实际问题。)然后,一旦您选择了其中一个下拉菜单,它就会触发一个带有答案的模式。目前,这只是一个概念验证,技术不是产品,所以这个想法只是创建一个带有一些预加载问题和模式的虚拟网站,而不是每次都实际查询数据库,
[当前状态] 我拼凑了一个自动完成搜索栏,并且我有一些功能模式。我不清楚的是如何让搜索触发模式。
这是我的搜索栏:
<form autocomplete="off" action="/action_page.php">
<div class="autocomplete" style="width:300px;">
<input id="myInput" type="text" name="myQuestion" placeholder="Ask a question.">
</div>
<input type="submit">
</form>
<script>
function autocomplete(inp, arr) {
/*the autocomplete function takes two arguments,
the text field element and an array of possible autocompleted values:*/
var currentFocus;
/*execute a function when someone writes in the text field:*/
inp.addEventListener("input", function(e) {
var a, b, i, val = this.value;
/*close any already open lists of autocompleted values*/
closeAllLists();
if (!val) { return false;}
currentFocus = -1;
/*create a DIV element that will contain the items (values):*/
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
/*append the DIV element as a child of the autocomplete container:*/
this.parentNode.appendChild(a);
/*for each item in the array...*/
for (i = 0; i < arr.length; i++) {
/*check if the item starts with the same letters as the text field value:*/
if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
/*create a DIV element for each matching element:*/
b = document.createElement("DIV");
/*make the matching letters bold:*/
b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
b.innerHTML += arr[i].substr(val.length);
/*insert a input field that will hold the current array item's value:*/
b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>";
/*execute a function when someone clicks on the item value (DIV element):*/
b.addEventListener("click", function(e) {
/*insert the value for the autocomplete text field:*/
inp.value = this.getElementsByTagName("input")[0].value;
/*close the list of autocompleted values,
(or any other open lists of autocompleted values:*/
closeAllLists();
});
a.appendChild(b);
}
}
});
/*execute a function presses a key on the keyboard:*/
inp.addEventListener("keydown", function(e) {
var x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div");
if (e.keyCode == 40) {
/*If the arrow DOWN key is pressed,
increase the currentFocus variable:*/
currentFocus++;
/*and and make the current item more visible:*/
addActive(x);
} else if (e.keyCode == 38) { //up
/*If the arrow UP key is pressed,
decrease the currentFocus variable:*/
currentFocus--;
/*and and make the current item more visible:*/
addActive(x);
} else if (e.keyCode == 13) {
/*If the ENTER key is pressed, prevent the form from being submitted,*/
e.preventDefault();
if (currentFocus > -1) {
/*and simulate a click on the "active" item:*/
if (x) x[currentFocus].click();
}
}
});
function addActive(x) {
/*a function to classify an item as "active":*/
if (!x) return false;
/*start by removing the "active" class on all items:*/
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
/*add class "autocomplete-active":*/
x[currentFocus].classList.add("autocomplete-active");
}
function removeActive(x) {
/*a function to remove the "active" class from all autocomplete items:*/
for (var i = 0; i < x.length; i++) {
x[i].classList.remove("autocomplete-active");
}
}
function closeAllLists(elmnt) {
/*close all autocomplete lists in the document,
except the one passed as an argument:*/
var x = document.getElementsByClassName("autocomplete-items");
for (var i = 0; i < x.length; i++) {
if (elmnt != x[i] && elmnt != inp) {
x[i].parentNode.removeChild(x[i]);
}
}
}
/*execute a function when someone clicks in the document:*/
document.addEventListener("click", function (e) {
closeAllLists(e.target);
});
}
var questions = ["How many widgets were sold in 2015?", "Where was the light bulb invented?", "What countries participated in the Hundred Years War?", "How many books are on the bookshelf?"];
/*initiate the autocomplete function on the "myInput" element, and pass along the questions array as possible autocomplete values:*/
autocomplete(document.getElementById("myInput"), questions);
</script>
解决方案
推荐阅读
- javascript - 文件中的摩卡访问变量我测试了哪个函数
- javascript - Rocket.Chat WebHook 与 Typescript
- java - 已解决 - 从 python 执行 java 文件
- angular - Angular RXJS 从另一种方法停止运行间隔
- postgresql - Liquibase 没有回滚数据库标签
- postgresql - 在 postgresql 中,如何按任意列表排序?
- visual-studio - VS 2019 中的这个功能名称是什么?
- drools - 了解 Phreak 算法的性能
- r - 如何为许多用户使用带有 get_timeline 的游标?
- laravel - 我收到此错误 TypeError: Cannot read property 'url' of null