简体   繁体   English

输入自动完成功能不适用于标签和值

[英]Input Auto-complete doesn't work with label & value

i use below code to input auto-complete, Since i have multiple value so i added label to display but after adding label my auto-complete function doesn't do anything. 我使用下面的代码输入自动完成功能,因为我有多个值,所以我添加了要显示的label ,但是添加label我的自动完成功能没有任何作用。

Can some one help me solve to this issue. 有人可以帮我解决这个问题吗?

 function autocomplete(inp, arr) { var currentFocus; inp.addEventListener("input", function (e) { var a, b, i, val = this.value; closeAllLists(); if (!val) { return false; } currentFocus = -1; a = document.createElement("DIV"); a.setAttribute("id", this.id + "autocomplete-list"); a.setAttribute("class", "autocomplete-items"); this.parentNode.appendChild(a); for (i = 0; i < arr.length; i++) { if (arr[i].substr(0, val.length).toUpperCase() === val.toUpperCase()) { b = document.createElement("DIV"); b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>"; b.innerHTML += arr[i].substr(val.length); b.innerHTML += "<input type='hidden' value='" + arr[i] + "'>"; b.addEventListener("click", function (e) { inp.value = this.getElementsByTagName("input")[0].value; closeAllLists(); }); a.appendChild(b); } } }); inp.addEventListener("keydown", function (e) { var x = document.getElementById(this.id + "autocomplete-list"); if (x) x = x.getElementsByTagName("div"); if (e.keyCode === 40) { currentFocus++; addActive(x); } else if (e.keyCode === 38) { currentFocus--; addActive(x); } else if (e.keyCode === 13) { e.preventDefault(); if (currentFocus > -1) { if (x) x[currentFocus].click(); } } }); function addActive(x) { if (!x) return false; removeActive(x); if (currentFocus >= x.length) currentFocus = 0; if (currentFocus < 0) currentFocus = (x.length - 1); x[currentFocus].classList.add("autocomplete-active"); } function removeActive(x) { for (var i = 0; i < x.length; i++) { x[i].classList.remove("autocomplete-active"); } } function closeAllLists(elmnt) { 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]); } } } document.addEventListener("click", function (e) { closeAllLists(e.target); }); } var countries = ["Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Anguilla", "Antigua & Barbuda", "Argentina", "Armenia"]; autocomplete(document.getElementById("myInput"), countries); 
 <div class="autocomplete" style="width:300px;"> <input id="myInput" type="text" name="myCountry" placeholder="Country"> <input type="hidden" name="Country"> </div> 

So to make label i done this and it doesn't work 所以要制作label我这样做了,但是不起作用

i changed 我变了

var countries = [{
                    "value": "Afghanistan, Pakistan",
                    "label": "Afghanistan"
                },
                {
                    "value": "Albania, Balkans",
                    "label": "Albania"
                }];

Note : i need to show label while typing and after selected because i need to store multiple input value. 注意 :我需要在键入时和选择后显示标签,因为我需要存储多个输入值。

This is how I fixed it: 这是我解决的方法:

var itemLabel = arr[i].label,
    itemValue = arr[i].value;

Use one of these wherever you are using arr[i] directly. 在您直接使用arr[i]任何地方都可以使用其中之一。 I am searching user-entered input on label but you can add on value , if needed. 我正在搜索label上用户输入的输入,但是如果需要,您可以添加value

You can check this fiddle for further details: https://jsfiddle.net/yeshas93/pg2dsj3u/1/ 您可以检查此提琴以获得更多详细信息: https : //jsfiddle.net/yeshas93/pg2dsj3u/1/

Hope this helps. 希望这可以帮助。 Please let me know in the comments if you have any doubts. 如果您有任何疑问,请在评论中让我知道。

Edit 编辑

Just for reference, you can check select2.js . 仅供参考,您可以检查select2.js It provides auto-complete feature with many other features on select-boxes. 它提供自动完成功能以及选择框上的许多其他功能。 Here is the link . 这是链接

Edit Reference https://jsfiddle.net/yeshas93/pg2dsj3u/ 编辑参考 https://jsfiddle.net/yeshas93/pg2dsj3u/

You can use datalist HTML to do this tag. 您可以使用数据列表HTML来执行此标记。

<input id="myInput" list="" type="text" name="myCountry" placeholder="Country">

<datalist id="countries">
  <option value="Afghanistan">
  <option value="Albania">
  <option value="Algeria">
</datalist>

In option tag, you can add all your countries list. 在选项标签中,您可以添加所有国家/地区列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM