aotocomplete1

<script type="text/javascript">
        $(function () {
            $("#txtSearch").autocomplete({
                minLength: 1, // 设置搜索的关键字最小长度
                // 设置自动完成列表的函数,函数包括两个参数,requset和response
                source: function (request, response) {
                    $.ajax({
                        type: "POST",
                        // 通过request.term可以获得文本框内容
                        url: "Handler1.ashx?keyword=" + encodeURIComponent(request.term),
                        contentType: "application/json; charset=gb2312",
                        dataType: "json",
                        success: function (data) {
                            // jQuery.map(array, callback) :将一个数组中的元素转换到另一个数组中。
                            //  下面就是把数组["value1", "value2",...]转换为[{value:"value1"}, {value:"value2"},...]
                            response($.map(data, function (item) {
                                return { value: item };
                            }));
                        },
                        error: function () {
                            alert("ajax请求失败");
                        }
                    });
                }
            });
        });
    </script>

原文地址:https://www.cnblogs.com/cjmtt/p/2920728.html