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

本文介绍了一个基于jQuery的自动补全功能实现方案。该方案通过前后端交互完成企业名称的实时匹配与显示,利用了jQuery插件autocomplete进行前端展示,并通过C#处理后端逻辑,从数据库中获取相应数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址:http://www.cnblogs.com/doublecc/archive/2009/12/05/1617381.html

前台页面代码:

<script type="text/javascript" src="../js/jquery.js" ></script>
<script type="text/javascript" src="../js/jquery.autocomplete.js"></script>
<script src="../js/thickbox.js" type="text/javascript"></script>
 <link href="../css/thickbox.css" rel="stylesheet" type="text/css" />
<link type="text/css" href="../css/jquery.autocomplete.css"  rel="Stylesheet"/>
<script type="text/javascript">
   //获取企业输入框ID
    var txtCorporation = "#<%=txtCorporation.ClientID %>";
   
    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("ListCorporation.ashx",
          {
            delay: 10,
            minChars: 1,
            matchSubset: 1,
            cacheLength: 1,
            onItemSelect: selectItem,
            onFindValue: findValue,
            autoFill: true,
            maxItemsToShow: 20
        });
    });   
</script>

<div>

<asp:TextBox id="txtCorporation" runat="server" ></asp:TextBox>

</div>

 后台页面代码:

<%@ WebHandler Language="C#" Class="ListCorporation" %>

using System;
using System.Web;
using System.Text;
using System.Data;
public class ListCorporation :
    IHttpHandler {
   
    public void ProcessRequest (HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        if (context.Request.QueryString["q"] != null)
        {
            string key =context.Request.QueryString["q"];
            context.Response.Write(GetCorporationList(key));
        }
      
       
    }
    public string GetCorporationList(string key)
    {
        com.hbwl.BLL.base_Corporation ListCorporation = new com.hbwl.BLL.base_Corporation();
        StringBuilder strWhere = new StringBuilder("1=1");
        strWhere.Append(" and CorporationName like '%").Append(key).Append("%'");
        DataTable dt = ListCorporation.GetList(10,1,strWhere.ToString()).Tables[0];
        string result = "";
           if(dt.Rows.Count>0)
            {
           
                StringBuilder items = new StringBuilder();
                //历遍所有的海关编码数据
                foreach (DataRow dr in dt.Rows)
                {
                    items.Append(dr["CorporationName"].ToString() + "\n");
                }
                result = items.ToString();
            }
            return result;
    } 
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值