当前位置: 代码迷 >> JavaScript >> ashx+jquery+autocomplete.js怎么实现自动填充
  详细解决方案

ashx+jquery+autocomplete.js怎么实现自动填充

热度:553   发布时间:2012-11-26 11:48:49.0
ashx+jquery+autocomplete.js如何实现自动填充

1.javascript:代码
<script src="js/jquery-1.7.min.js" type="text/javascript"></script>
    <script src="js/jquery.autocomplete.min.js" type="text/javascript"></script>
    <link href="../css/jquery.autocomplete.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
        function findValue(li) {
            if (li == null) return alert("No match!");
            if (!!li.extra)
                var sValue = unescape(li.extra[0]);

        }
        function selectItem(li) {
            findValue(li);
        }

        $(document).ready(function () {
            $(txtCorporation).autocomplete("../Handler/SearchKeyValue.ashx",
          {
              delay: 10,
              minChars: 1,
              matchSubset: 1,
              cacheLength: 1,
              onItemSelect: selectItem,
              onFindValue: findValue,
              autoFill: true,
              maxItemsToShow: 20
          });
        });    

    </script>

2.ashx文件内容

public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (context.Request.QueryString["q"] != null)
            {
                string key = context.Request.QueryString["q"];
                string keyvalues = GetKeyValues(key);
                context.Response.Write(keyvalues);
            }
        }
        public string GetKeyValues(string k)
        {
            try
            {
                DataTable dt = BLL.GetDataTable("select KeyValue from wb_SearchKeyValue where KeyValue like '%"+k+"%'");
                string result = "";
                if (!CommonClass.DTRow.CheckDtIsEmpty(dt))
                {
                   StringBuilder items = new StringBuilder();
                    foreach (DataRow dr in dt.Rows)
                    {
                        items.Append(dr["KeyValue"].ToString() + "\n");
                    }
                    result = items.ToString();
                }
                return result;
            }
            catch (Exception ex)
            {
                AppLog.Write("关键字获取异常![SearchKeyValue.ashx异常信息:" + ex.Message + "]", AppLog.LogMessageType.Info);
                return string.Empty;
            }
  


  相关解决方案