input autocomplete 下拉提示+支持中文

时间:2021-05-19 03:17:31

js 代码:

$.getJSON("/Foreign/Getforeign_routeEndPoint", function (data) {
            $("#NameReturn_Arch").autocomplete(data, {
                minChars: 0, //表示在自动完成激活之前填入的最小字符
                max: 100, //表示列表里的条目数
                autoFill: true, //表示自动填充
                mustMatch: true, //表示必须匹配条目,文本框里输入的内容,必须是data参数里的数据,如果不匹配,文本框就被清空
                matchContains: false, //表示包含匹配,相当于模糊匹配
                scrollHeight: 200, //表示列表显示高度,默认高度为180
                dataType: "json", //数据格式      

                formatItem: function (row) {
                    return row.text;
                },
                formatMatch: function (row) {
                    return row.text;
                },
                formatResult: function (row) {
                    return row.text;
                }
            });
        });

 

后台:

    public ActionResult Getforeign_routeEndPoint()
        {
            List<foreign_route> List = for_rou.GetRouteCodeName("EndPoint").ToList();
            List<SelectText> ulist = new List<SelectText>();
            if (List.Count > 0)
            {
                string[] endPoint;
                SelectText text;
                foreach (var i in List)
                {
                    endPoint = i.EndPoint.Split('、');
                    if (endPoint.Length > 1)
                    {
                        foreach (string t in endPoint)
                        {
                            text = new SelectText { id = t, text = t };
                            if (!ulist.Any(x=>x.id==t&&x.text==t))
                            {
                                ulist.Add(text);
                            }
                        }
                    }
                    else
                    {
                        text = new SelectText { id = i.EndPoint, text = i.EndPoint };
                        if (!!ulist.Any(x => x.id == i.EndPoint && x.text == i.EndPoint))
                        {
                            ulist.Add(text);
                        }
                    }
                }
            }

            return Json(ulist, JsonRequestBehavior.AllowGet);
        }

 

由于有的浏览器不兼容,所以需要修改一下jquery.autocomplete.js代码

在js脚本里面的199行或者200行添加修改

.bind("input", function() {  
            onChange(0, true);  
        }).bind("unautocomplete", function() {
        select.unbind();
        $input.unbind();
        $(input.form).unbind(".autocomplete");
    });