首页 > 代码库 > jquery autocompelete

jquery autocompelete

 前台调用:

     $("#CompanyName").autocomplete("../AjaxHandler/GetBranchH.ashx", {
        formatItem: function(row, i,max) {
            var obj =eval("(" + row + ")"); //转换成js对象
            return obj.COMPANYNAME;
        },
        formatResult: function(row) {
            var obj =eval("(" + row + ")"); //转换成js对象
            return obj.COMPANYNAME;
        }
        }).result(function(event, item) {
            var obj = eval("(" +item + ")"); //转换成js对象
            //alert(obj.BRANCHCOMPANYNO);
            var html="";
            $("#department").empty();
            $("#BRANCHCOMPANYNO").val(obj.BRANCHCOMPANYNO);
            $.getJSON("../AjaxHandler/GetByNoDepartmentH.ashx",{Num:obj.BRANCHCOMPANYNO},function(data){
               $.each(data,function(k,v){
                   html+="<option value="http://www.mamicode.com/+v.DEPID+">"+v.DEPARTMENTNAME+"</option>";
               });
              $("#department").append(html);
            });
            
            //$("#link").attr("href",obj.url);
      });

后台代码:

            string strwhere="BRANCHCOMPANYNO like ‘%"+param+"%‘";
            strwhere += " or COMPANYNAME like ‘%" + param + "%‘";
            strwhere += " or SHORTNAME like ‘%" + param + "%‘";
            DataTable dt= b_Branch.GetList(15,strwhere,"ID").Tables[0];
            
            //JavaScriptSerializer jss = new JavaScriptSerializer();
            //List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();

            //context.Response.Write("[{\"label\":\"博客园\",\"value\":\"cnblogs\"},{\"label\":\"囧月\",\"value\":\"囧月\"}]");

            HttpResponse response = context.Response;
            System.IO.TextWriter writer = response.Output;
            // 注意,必须是标准的 JSON 格式串,必须使用双引号
            //writer.Write("[\"One\", \"Two\", \"Three\"]");
            string strb = "";
            foreach (DataRow dr in dt.Rows)
            {
                //Dictionary<string, object> result = new Dictionary<string, object>();
                foreach (DataColumn dc in dt.Columns)
                {
                    if (dc.ColumnName == "BRANCHCOMPANYNO") {
                        strb+="{BRANCHCOMPANYNO:\"" + dr[dc.ColumnName].ToString() + "\",";
                    }
                    if (dc.ColumnName == "COMPANYNAME")
                    {
                        strb += "COMPANYNAME:\"" + dr[dc.ColumnName].ToString() + "\"}\n";
                        //result.Add(dc.ColumnName, dr[dc.ColumnName].ToString());
                    }
                }
                //list.Add(result);
            }
            strb = strb.ToString().TrimEnd(‘\n‘);
            //strb += "]";
            writer.Write(strb);

jquery autocompelete