我的jquery自动完成功能不起作用

时间:2015-10-14 07:57:13

标签: javascript c# jquery asp.net autocomplete

我使用jquery为文本框创建自动完成,从asmx webservice获取数据。我在firebug上监视我的代码,这个工具显示发送请求并收到xml响应。但自动填充功能未打开文本框:(
有人可以告诉我为什么我的jquery自动完成代码不起作用吗? jquery代码:

<link href="../Script/MainCSS.css" rel="stylesheet" />
<link href="../Script/jquery-ui.css" rel="stylesheet" />
<script src="../Script/jquery-1.10.2.js"></script>
<script src="../Script/jquery-ui.js"></script>
<script type="text/javascript">
    $(function () {
        $('.textBox').autocomplete({
            source: function (request, response) {               
                $.ajax({
                    url: "/Services/BusService.asmx/GetRouteInfo",
                    data: { param: $('.textBox').val() },
                    dataType: "json",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataFilter: function (data) { return data; },
                    success: function (data) {
                        response($.map(data.d, function (item) {
                            return {
                                value: item
                            }
                        }))
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        var err = eval("(" + XMLHttpRequest.responseText + ")");
                        alert(err.Message)
                        // console.log("Ajax Error!");  
                    }
                });
            },
            minLength: 2 //This is the Char length of inputTextBox  
        });
    });
</script>

我的c#代码:

 [WebMethod]
        public string[] GetRouteInfo(string param)
        {
            List<string> list_result = new List<string>();

            AutoTaxiEntities useEntity = new AutoTaxiEntities();

            System.Data.Entity.Core.Objects.ObjectResult<DAL.Model.SP_FIND_ROUTE_ROUTESTOPS_Result> sp_result = useEntity.SP_FIND_ROUTE_ROUTESTOPS(param);

            foreach (DAL.Model.SP_FIND_ROUTE_ROUTESTOPS_Result sp_result_item in sp_result.ToList())
            {
                list_result.Add(sp_result_item.ID + "," + sp_result_item.ITEMTYPE + "," + sp_result_item.TITLE);
            }

            return list_result.ToArray();

        }

2 个答案:

答案 0 :(得分:1)

查看添加的脚本引用,我认为您尚未添加autocomplete.js脚本。请添加并尝试一次。Please click here

答案 1 :(得分:0)

 $(document).ready(function () {
        $('#<%=txtSearch.ClientID%>').autocomplete({
            source: function (request, response) {
                $.ajax({
                    type: "POST",
                    url: "/Services/BusWebService.asmx/GetRouteInfo",
                    data: "{ 'param': '" + request.term + "' }",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    dataFilter: function (data) { return data; },
                    success: function (data) {
                        response($.map(data.d, function (item) {
                            return {
                                itemid: item.split(',')[0],
                                itemtype: item.split(',')[1],
                                label: item.split(',')[2]
                            }
                        }))
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(textStatus);
                    }
                });
            },
            minLength: 2,
            select: function (event, ui) {
                $('#<%=hfItem.ClientID%>').val(ui.item.itemid + ',' + ui.item.itemtype + ',' + ui.item.label);
                //$("form").submit();
            }
        });
  });