Javascript 如何在单击输入时显示下拉列表
我希望在单击输入时打开下拉列表,这样即使不键入,我们也可以选择选项Javascript 如何在单击输入时显示下拉列表,javascript,php,wordpress,Javascript,Php,Wordpress,我希望在单击输入时打开下拉列表,这样即使不键入,我们也可以选择选项 请参见输入自动完成示例您可以将显示自动完成列表的代码行剪切到单独的函数: function showList(inp, arr, val) { /*create a DIV element that will contain the items (values):*/ a = document.createElement("DIV"); a.setAttribute("id", this.id + "
请参见输入自动完成示例您可以将显示自动完成列表的代码行剪切到单独的函数:
function showList(inp, arr, val) {
/*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:*/
inp.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);
}
}
}
下面是正在运行的代码片段
功能显示列表(inp、arr、val){
/*创建将包含以下项(值)的DIV元素:*/
a=document.createElement(“DIV”);
a、 setAttribute(“id”,this.id+“自动完成列表”);
a、 setAttribute(“类”、“自动完成项”);
/*将DIV元素作为自动完成容器的子元素追加:*/
inp.parentNode.appendChild(a);
/*对于数组中的每个项*/
对于(i=0;i”+arr[i].substr(0,val.length)+“”;
b、 innerHTML+=arr[i].substr(val.length);
/*插入将保存当前数组项值的输入字段:*/
b、 innerHTML+=“”;
/*当有人单击项值(DIV元素)时执行函数:*/
b、 addEventListener(“单击”,函数(e){
/*插入自动完成文本字段的值:*/
inp.value=this.getElementsByTagName(“输入”)[0].value;
/*关闭自动完成值的列表,
(或任何其他打开的自动完成值列表:*/
closeAllList();
});
a、 儿童(b);
}
}
}
功能自动完成(inp、arr){
/*autocomplete函数接受两个参数,
文本字段元素和可能的自动完成值数组:*/
无功电流聚焦;
/*当有人在文本字段中写入时执行函数:*/
inp.addEventListener(“输入”,函数(e){
var a,b,i,val=该值;
/*关闭所有已打开的自动完成值列表*/
closeAllList();
如果(!val){返回false;}
currentFocus=-1;
显示列表(inp、arr、val);
});
inp.addEventListener(“焦点”,函数(e){
closeAllList();
显示列表(inp,arr,“”);
});
/*按键盘上的键执行功能:*/
inp.addEventListener(“向下键控”,函数(e){
var x=document.getElementById(this.id+“自动完成列表”);
如果(x)x=x.getElementsByTagName(“div”);
如果(e.keyCode==40){
/*如果按下向下箭头键,
增加currentFocus变量:*/
currentFocus++;
/*并使当前项目更加可见:*/
addActive(x);
}如果(e.keyCode==38){//up
/*如果按下向上箭头键,
减小currentFocus变量:*/
当前焦点--;
/*并使当前项目更加可见:*/
addActive(x);
}否则如果(e.keyCode==13){
/*如果按ENTER键,则阻止提交表单*/
e、 预防默认值();
如果(当前焦点>-1){
/*并模拟单击“活动”项:*/
如果(x)x[currentFocus]。单击();
}
}
});
函数addActive(x){
/*将项目分类为“活动”的函数:*/
如果(!x)返回false;
/*首先删除所有项目上的“活动”类:*/
清除活性(x);
如果(currentFocus>=x.length)currentFocus=0;
如果(currentFocus<0)currentFocus=(x.length-1);
/*添加类“自动完成活动”:*/
x[currentFocus].classList.add(“自动完成活动”);
}
函数removeActive(x){
/*从所有自动完成项中删除“活动”类的函数:*/
对于(变量i=0;i /*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;
showList(inp, arr, val);
});
/*execute a function when focus on text field:*/
inp.addEventListener("focus", function(e) {
closeAllLists();
showList(inp, arr, "");
});
<select>
<option value="test1">test1</option>
<option value="test2">test2</option>
</select>