代码之家  ›  专栏  ›  技术社区  ›  sanoj lawrence

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

  •  -1
  • sanoj lawrence  · 技术社区  · 7 年前

    我使用下面的代码输入auto-complete,因为我有多个值,所以我添加了 label 显示但添加后 标签 我的自动完成功能不起作用。

    有人能帮我解决这个问题吗?

    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>

    所以要做 标签 我做了这件事但不起作用

    我变了

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

    注意 :我需要在键入和选择后显示标签,因为我需要存储多个输入值。

    2 回复  |  直到 7 年前
        1
  •  1
  •   yeshashah    7 年前

    这就是我修复它的方法:

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

    在任何你使用的地方使用其中一个 arr[i] 直接。我正在搜索用户输入的输入 label 但是你可以加上 value ,如果需要。

    您可以查看此小提琴以了解更多详细信息: https://jsfiddle.net/yeshas93/pg2dsj3u/1/

    希望这有帮助。如果你有任何疑问,请在评论中告诉我。

    编辑

    仅供参考,您可以查看 select2.js .它提供自动完成功能,并在选择框中提供许多其他功能。 Here is the link .

    编辑引用 https://jsfiddle.net/yeshas93/pg2dsj3u/

        2
  •  -1
  •   MANISH KUMAR SINGH    7 年前

    您可以使用datalist html来做这个标记。

    <input id="myInput" list="" type="text" name="myCountry" placeholder="Country">
    
    <datalist id="countries">
      <option value="Afghanistan">
      <option value="Albania">
      <option value="Algeria">
    </datalist>
    

    在选项标签中,您可以添加所有国家/地区列表。