欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-22 19:45:06
这是我在公司项目中遇到的一个需求问题。大致是一个表单页面,人员需要填写基本信息,其中需要选择"学历"和"专业类别",由这两个变量去确定"所学专业"的一个范围。用户在所学专业输入框输入内容时,会自动联想、补充出剩余的字。所学专业的内容保存在数据库当中,当联想不出来相应的专业名称时,用户自行填写。自动联想的功能解决方案参考的是菜鸟教程。直达链接1、autocomplete.jsfunction autocomplete(inp, arr) { /*函数主要有两个参数:文本框元素和自动补齐的...

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

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


1、autocomplete.js

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");
        /*添加 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数据如下如:

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

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

6、最终实现效果

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

本文地址:https://blog.csdn.net/a1085578081/article/details/107384589

相关标签: javascript