Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JavaScript中自动完成显示条目而不考虑词序_Javascript_Autocomplete - Fatal编程技术网

如何在JavaScript中自动完成显示条目而不考虑词序

如何在JavaScript中自动完成显示条目而不考虑词序,javascript,autocomplete,Javascript,Autocomplete,我正在尝试制作一个JavaScript自动完成函数。下面是我的JavaScript代码: 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 whe

我正在尝试制作一个JavaScript自动完成函数。下面是我的JavaScript代码:

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);
});
} 

问题是,当我必须键入完整的输入,例如“美利坚合众国”,以便填充下拉列表时。如果我只输入“美国”,我无法得到结果来填充。如何更改自动完成功能,以便键入的单词顺序无关紧要?

您必须测试国家名称是否包含输入,而不是测试国家是否以输入开头。
我在自动完成函数中为重写了
,如下所示:

valPosition=arr[i].toUpperCase().indexOf(val.toUpperCase());
对于(i=0;i”+arr[i].substr(valPosition,valPosition+val.length)+“”;
b、 innerHTML+=arr[i].substr(val.length);
/*插入将保存当前数组项值的输入字段:*/
b、 innerHTML+=“”;
/*当有人单击项值(DIV元素)时执行函数:*/
b、 addEventListener(“单击”,函数(e){
/*插入自动完成文本字段的值:*/
inp.value=this.getElementsByTagName(“输入”)[0].value;
/*关闭自动完成值的列表,
(或任何其他打开的自动完成值列表:*/
closeAllList();
});
a、 儿童(b);
}
}

您需要检查输入值是否存在于任何国家/地区值中

const arrVal = arr[i].toLowerCase();
const searchVal = val.toLowerCase();
if (arrVal.includes(searchVal))
您还需要稍微更改标记的搜索文本代码

const text = arr[i].substring(0, matchStartIndex) + "<strong>" + arr[i].substring(matchStartIndex, matchStartIndex+searchVal.length) + "</strong>" + arr[i].substring(matchStartIndex+searchVal.length);
const text=arr[i]。子字符串(0,matchStartIndex)+“”+arr[i]。子字符串(matchStartIndex,matchStartIndex+searchVal.length)+“”+arr[i]。子字符串(matchStartIndex+searchVal.length);
var国家=[“台湾”、“塔吉克斯坦”、“坦桑尼亚”、“泰国”、“东帝汶”、“多哥”、“汤加”、“特立尼达和多巴哥”、“突尼斯”、“土耳其”、“土库曼斯坦”、“特克斯和凯科斯群岛”、“图瓦卢”、“乌干达”、“乌克兰”、“阿拉伯联合酋长国”、“联合王国”、“美利坚合众国”、“乌拉圭”、“乌兹别克斯坦”、“瓦努阿图”、“梵蒂冈城”、“委内瑞拉”、“越南”,“维尔京群岛(美国)”、“也门”、“赞比亚”、“津巴布韦”];
自动完成(document.getElementById(“myInput”),国家/地区);
功能自动完成(inp、arr){
/*autocomplete函数接受两个参数,
文本字段元素和可能的自动完成值数组:*/
无功电流聚焦;
/*当有人在文本字段中写入时执行函数:*/
inp.addEventListener(“输入”,函数(e){
var a,b,i,val=该值;
/*关闭所有已打开的自动完成值列表*/
closeAllList();
如果(!val){返回false;}
currentFocus=-1;
/*创建将包含以下项(值)的DIV元素:*/
a=document.createElement(“DIV”);
a、 setAttribute(“id”,this.id+“自动完成列表”);
a、 setAttribute(“类”、“自动完成项”);
/*将DIV元素作为自动完成容器的子元素追加:*/
this.parentNode.appendChild(a);
/*对于数组中的每个项*/
对于(i=0;i”+arr[i]。子字符串(matchStartIndex,matchStartIndex+searchVal.length)+“”+arr[i]。子字符串(matchStartIndex+searchVal.length);
b、 innerHTML+=文本;
/*插入将保存当前数组项值的输入字段:*/
b、 innerHTML+=“”;
/*当有人单击项值(DIV元素)时执行函数:*/
b、 addEventListener(“单击”,函数(e){
/*插入自动完成文本字段的值:*/
inp.value=this.getElementsByTagName(“输入”)[0].value;
/*关闭自动完成值的列表,
(或任何其他打开的自动完成值列表:*/
closeAllList();
});
a、 儿童(b);
}
}
});
/*按键盘上的键执行功能:*/
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);
const arrVal = arr[i].toLowerCase();
const searchVal = val.toLowerCase();
if (arrVal.includes(searchVal))
const text = arr[i].substring(0, matchStartIndex) + "<strong>" + arr[i].substring(matchStartIndex, matchStartIndex+searchVal.length) + "</strong>" + arr[i].substring(matchStartIndex+searchVal.length);