Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.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
Autocomplete Javascript自动完成与来自服务器C的动态数据#_Autocomplete - Fatal编程技术网

Autocomplete Javascript自动完成与来自服务器C的动态数据#

Autocomplete Javascript自动完成与来自服务器C的动态数据#,autocomplete,Autocomplete,我有一个javascript数组,我在每次搜索中都会更新该数组以用于自动完成。 数据从服务器返回,但自动完成功能无法正常工作 HTML: <input id="myInput" class="center-block" placeholder="Enter Text..." onkeyup="updateAutocomplete()" /> Javascript <script> funct

我有一个javascript数组,我在每次搜索中都会更新该数组以用于自动完成。 数据从服务器返回,但自动完成功能无法正常工作

HTML:

<input id="myInput" class="center-block" placeholder="Enter Text..." onkeyup="updateAutocomplete()" />
Javascript

<script>
function autocomplete(inp) {



           /*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 < countries.length; i++) {
                   /*check if the item starts with the same letters as the text field value:*/
                   if (countries[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>" + countries[i].substr(0, val.length) + "</strong>";
                       b.innerHTML += countries[i].substr(val.length);
                       /*insert a input field that will hold the current array item's value:*/
                       b.innerHTML += "<input type='hidden' value='" + countries[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 countries = [];
       autocomplete(document.getElementById("myInput"));

       function updateAutocomplete() {


           var part = document.getElementById("myInput").value;

           if (part != "") {
               $.ajax({
                   type: "POST",
                   url: "index.aspx/GetAutocomplete",
                   data: "{'part':'" + part + "'}",
                   dataType: "json",
                   contentType: "application/json; charset=utf-8",
                   success: function (data) {

                       var returnedstring = data.d;


                       if (returnedstring == "ERR") {
                       }
                       else {
                           countries = returnedstring.split('|');
                       }
                   }
               });
           }
       }

</script>

函数自动完成(inp){
/*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”+国家[i].substr(0,val.length)+“”;
b、 innerHTML+=国家[i].substr(val.length);
/*插入将保存当前数组项值的输入字段:*/
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);
如果(currentFocus>=x.length)currentFocus=0;
如果(currentFocus<0)currentFocus=(x.length-1);
/*添加类“自动完成活动”:*/
x[currentFocus].classList.add(“自动完成活动”);
}
函数removeActive(x){
/*从所有自动完成项中删除“活动”类的函数:*/
对于(变量i=0;i<script>
function autocomplete(inp) {



           /*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 < countries.length; i++) {
                   /*check if the item starts with the same letters as the text field value:*/
                   if (countries[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>" + countries[i].substr(0, val.length) + "</strong>";
                       b.innerHTML += countries[i].substr(val.length);
                       /*insert a input field that will hold the current array item's value:*/
                       b.innerHTML += "<input type='hidden' value='" + countries[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 countries = [];
       autocomplete(document.getElementById("myInput"));

       function updateAutocomplete() {


           var part = document.getElementById("myInput").value;

           if (part != "") {
               $.ajax({
                   type: "POST",
                   url: "index.aspx/GetAutocomplete",
                   data: "{'part':'" + part + "'}",
                   dataType: "json",
                   contentType: "application/json; charset=utf-8",
                   success: function (data) {

                       var returnedstring = data.d;


                       if (returnedstring == "ERR") {
                       }
                       else {
                           countries = returnedstring.split('|');
                       }
                   }
               });
           }
       }

</script>