惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
P
Proofpoint News Feed
Hugging Face - Blog
Hugging Face - Blog
MyScale Blog
MyScale Blog
A
About on SuperTechFans
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
The Blog of Author Tim Ferriss
MongoDB | Blog
MongoDB | Blog
博客园 - 【当耐特】
The Cloudflare Blog
F
Fortinet All Blogs
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
宝玉的分享
宝玉的分享
罗磊的独立博客
量子位
有赞技术团队
有赞技术团队
V
V2EX
Engineering at Meta
Engineering at Meta

博客园 - 李帅斌-Memory

快速导出CSV log4net使用详解 . C# 格式化字符串 web.config中特殊字符的转译 transactionscope的使用 一些概念需要明确的解释一下 还原已清空的回收站 安装卸载Windows服务 Jquery 文章积累 Spring.NET学习——控制反转(基础篇) SqlServer2005导出数据到SqlServer2000步骤! - 李帅斌-Memory - 博客园 MSSQL2005:SQL Server 2005安装图解 网站优化-Gzip压缩 Asp.Net读取excel文件的时候 出错提示:外部表不是预期的格式 解决方案 js触发回车事件 - 李帅斌-Memory - 博客园 在Div中包含的元素移动时,触发div的onmouseout事件.解决方案! - 李帅斌-Memory - 博客园 VisualStudio用IE8调试时遇到的问题 Asp.net页面执行两遍Page_Load的解决方案 C#取硬盘序列号(-备忘-)
JQuery+Ajax实现无刷新数据查询
李帅斌-Memory · 2009-05-07 · via 博客园 - 李帅斌-Memory

闲来无事,写了一个无刷新数据查询的小功能,来显摆显摆. 类似baidu、google的搜索提示功能。hoho

实现原理,在文本框输入值时,异步调用后台数据,方法用JQuery中的$.get().后台数据数据部分,接收到URL传过来的参数后,查询存储在XML中的数据.如果匹配,则输入出来.很简单的一个小功能!孜当练习了.hoho!!!

如图所示:

1、输入汉语拼音:

      在文本框中输入li,以后li为开头的汉语拼音的中文字符都会提示出来。

2、输入汉字:

      在文本框中输入中文字符,例如李,提示信息就出显示出以李开头的所有信息。

以下是代码:

HTML代码部分
.divTip
{
 font-size: 12px;
 width: 100%;
 font-family: Arial;
 border: dashed 1px #d7d7d7;
 position:static;
 margin: 20px 0 0 0;
 height:30px;
 line-height:30px;
 background-color:#f5f5f5;
 }

#main
{
 width: 400px;
 height:400px;
 border: solid 1px #06c;
 position: absolute;
 top: 50%;
 left:50%;
 margin: -200px 0 0 -200px;

 }
#searchBox
{
 position: absolute;
 margin: 0 0 0 0;
 }
 

<body>
    <form id="form1" runat="server">

      <div id="main">
        <input id="searchBox" type="text" onpropertychange="searchJsHelper.ShowSearchKeyTip(this)" oninput="searchJsHelper.ShowSearchKeyTip(this)" />
        <div id="searchResult">
            请输入要查询的姓名
        </div>
    </div>

</form>
</body>

JavaScript 部分

var SearchJs = function(){}

SearchJs.prototype.ShowSearchKeyTip = function(o){
    var t;
    if(o.value == "")
    {
        $("#searchResult").css("display","none");
        return ;
    }
    $("#searchResult").css("display","block");
   
    $.get("SearchHandler.ashx?key=" + encodeURIComponent(o.value), function(data){
        var arr = data.split("|");
        $("#searchResult").empty();
        for(var i=0;i<arr.length;i++)
        {
            t = setTimeout('searchJsHelper.HiddenSearchKeyTip()',10000);
            var d = document.createElement("DIV");
            d.style.cursor = "hand";
            d.id="div" + i;
           
            d.className = "divTip";
            d.innerText = arr[i];
           
            $("#"+ d.id).mouseover(function(){
                clearTimeout(t);
                $("#"+ d.id).css("display","block");
            });
            $("#"+ d.id).mouseout(function() {
                t = setTimeout('searchJsHelper.HiddenSearchKeyTip()',10000);
            });    
           
            $("#searchResult").append(d);
        }
    });
   
    $("#searchResult").mouseover(function(){
        clearTimeout(t);
        $("#searchResult").css("display","block");
    });
    $("#searchResult").mouseout(function() {
        t = setTimeout('searchJsHelper.HiddenSearchKeyTip()',10000);
    });           
}

SearchJs.prototype.HiddenSearchKeyTip = function(o){
    $("#searchResult").css("display","none");
}

//建立scriptHelper类的一个实例对象.全局使用.
var searchJsHelper = new SearchJs();

后台数据部分:

SearchHandler.ashx

      public String SearchKey
        {
            get{
                if (HttpContext.Current.Request.QueryString["key"] != null)
                {
                    return HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.QueryString["key"].ToString());
                }
                else
                    return string.Empty;
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            StringBuilder sb =new StringBuilder(1024*500);

            XDocument xml = XDocument.Load(context.Server.MapPath("PeopleMessages.xml"));
            var peoples = from p in xml.Root.Elements("people")
                          select new
                          {
                              enname = p.Element("ENName").Value,
                              cnname = p.Element("CNName").Value,
                              message = p.Element("message").Value
                          };
            foreach (var p in peoples)
            {
                var s = p.enname.Trim();
                var c = p.cnname.Trim();
                var m = p.message.Trim();
                if (s.Length >= SearchKey.Length)
                {
                    if (SearchKey == s.Substring(0, SearchKey.Length))
                    {
                        sb.Append(c + ": " + m + "|");
                    }
                }
                if (c.Length >= SearchKey.Length)
                {
                    if (SearchKey == c.Substring(0, SearchKey.Length))
                    {
                        sb.Append(c + ": " + m + "|");
                    }
                }
            }
           
            context.Response.ContentType = "text/plain";
            context.Response.Write(sb.ToString().Substring(0, sb.ToString().Length - 1));
        }

 XML数据部分:

<?xml version="1.0" encoding="utf-8" ?>
<Messages>
 <people>
  <ENName>lishuaibin</ENName>
  <CNName>李帅斌</CNName>
  <message>
   我只是想做个程序员!
  </message>
 </people>
 <people>
  <ENName>zhangxianbo</ENName>
  <CNName>张三</CNName>
  <message>
   我是张三,我也只是想做个程序员!
  </message>
 </people>
 <people>
  <ENName>linsi</ENName>
  <CNName>林四</CNName>
  <message>
   hoho,我是个牛人!
  </message>
 </people>
 <people>
  <ENName>jiangwu</ENName>
  <CNName>姜五</CNName>
  <message>
   我也是牛人,哪说理去呀!
  </message>
 </people>
 <people>
  <ENName>liuliu</ENName>
  <CNName>刘六</CNName>
  <message>
   你们脸真大!
  </message>
 </people>
</Messages>