JS输入框添加联想、自动补充功能

这是我在公司项目中遇到的一个需求问题。大致是一个表单页面,人员需要填写基本信息,其中需要选择"学历"和"专业类别",由这两个变量去确定"所学专业"的一个范围。用户在所学专业输入框输入内容时,会自动联想、补充出剩余的字。所学专业的内容保存在数据库当中,当联想不出来相应的专业名称时,用户自行填写。

自动联想的功能解决方案参考的是菜鸟教程。直达链接


1、autocomplete.js

function autocomplete(inp, arr) {
    /*函数主要有两个参数:文本框元素和自动补齐的完整数据*/
    var currentFocus;

/* 监听 - 在有焦点时触发 */
    inp.addEventListener("focus", function(e) {
        var a, b, i, val = this.value;
        /*关闭已经打开的自动完成值列表*/
        closeAllLists();
        if (!val) {
            /*创建列表*/
            a = document.createElement("DIV");
            a.setAttribute("id", this.id + "autocomplete-list");
            a.setAttribute("class", "autocomplete-items");
            /*添加 DIV 元素*/
            this.parentNode.appendChild(a);
            /*循环数组...*/
            for (i = 0; i < arr.length; i++) {
                /*检查选项是否以与文本字段值相同的字母开头*/
                    /*为匹配元素创建 DIV*/
                    b = document.createElement("DIV");
                    /*使匹配字母变粗体*/
                    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);
                }
            return ;
        }
        currentFocus = -1;
        /*创建列表*/
        a = document.createElement("DIV");
        a.setAttribute("id", this.id + "autocomplete-list");
        a.setAttribute("class", "autocomplete-items");
        /*添加 DIV 元素*/
        this.parentNode.appendChild(a);
        /*循环数组...*/
        for (i = 0; i < arr.length; i++) {
            /*检查选项是否以与文本字段值相同的字母开头*/
            if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
                /*为匹配元素创建 DIV*/
                b = document.createElement("DIV");
                /*使匹配字母变粗体*/
                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.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");
        /*添加 DIV 元素*/
        this.parentNode.appendChild(a);
        /*循环数组...*/
        for (i = 0; i < arr.length; i++) {
            /*检查选项是否以与文本字段值相同的字母开头*/
            if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
                /*为匹配元素创建 DIV*/
                b = document.createElement("DIV");
                /*使匹配字母变粗体*/
                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);
    });
}

2、autocomplete.css

.autocomplete {
	/*the container must be positioned relative:*/
	position: relative;
	display: inline-block;
}
.autocomplete-items {
	position: absolute;
	border: 1px solid #d4d4d4;
	border-bottom: none;
	border-top: none;
	z-index: 99;
	/*position the autocomplete items to be the same width as the container:*/
	top: 100%;
	left: 15px;
	right: 15px;
}
.autocomplete-items div {
	padding: 10px;
	cursor: pointer;
	background-color: #fff;
	border-bottom: 1px solid #d4d4d4;
}
.autocomplete-items div:hover {
	/*when hovering an item:*/
	background-color: #e9e9e9;
}
.autocomplete-active {
	/*when navigating through the items using the arrow keys:*/
	background-color: DodgerBlue !important;
	color: #ffffff;
}

3、h5代码

<div class="form-group">
            <label class="col-sm-4 control-label">所学专业:</label>
            <div class="col-sm-8 autocomplete">
                <input type="text" name="major" class="form-control auto-inp" placeholder="请输入所学专业" id="professional">
            </div>
        </div>

4、JS调用代码

//联想词的列表,初始为空
var professionalList = [];

autocomplete(document.getElementById("professional"),professionalList);

5、需求说的"学历"和"专业类别"变化时

function changeProfessionalList() {
        var data = {
            "educationBackgroundCode":$("#educationBackground").val(),
            "academicDirectionsCode":$("#academicDirections").val()
        }
        $.ajax({
            url: ctx + "/sys/prefessional/getPrefessional",
            type: 'post',
            headers: {
                'Content-Type': 'application/json'
            },
            data: JSON.stringify(data)
        }).done(function (result) {
            //标注
            professionalList.splice(0,professionalList.length);
            for (var i = 0;i<result.length ;i++){
                professionalList.push(result[i]);
            }
        })
    }

此处我从数据库取到的Json数据如下如:

注意:在ajax动作获取数据完成时(即代码备注标注处),在将数据赋值给外面的professionalList时,我一开始直接使用professionalList = result;  运行都没问题,但是自动联想、补充的功能就是出不来。用开发者选项调试 professionalList也有改变过来。后面我才用将列表的数据都清除,再将result的数据一个一个push到professionalList中就可以了。

6、最终实现效果

 

 


2020-10-10补充:在ie浏览器中,因为我添加了两个监听,分别是focus和input。在ie中当触发foucs事件的时候会触发input事件,这就很奇怪,会导致我先触发的focus事件会被input覆盖掉,导致我需要点击两次所学专业才能选择成功。

解决方法:将placeholder去掉即可

详解:直达

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值