智能搜索

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script type="text/javascript" >
        function FunSelect() {
            var word = document.getElementById("city").value;
            if (word != "") {
                // alert("1");
                var xmlhttp = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
                //  alert("2");
                xmlhttp.open("GET", "Handler1.ashx?word=" + encodeURIComponent(word), true);
                //  alert("3")
                xmlhttp.send(null);
                //   alert("4");
                xmlhttp.onreadystatechange = function () {
                    if (xmlhttp.readyState == 4) {
                       // alert(xmlhttp.responseText);
                       document.getElementById("myText").innerHTML = xmlhttp.responseText;
                      
                    }
                }
            }
          
        }
        function FunGetValue(s) {
            document.getElementById("city").value = s;
            document.getElementById("myText").innerHTML = "";
        }
    </script>
    <style>
        li:hover {
            background-color:#b6ff00;
        }
        #divFeild {
            500px;
            height:20px;
            position:relative;
        }
        #wordDiv {
            position:absolute;
            left:20px;
            top:20px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div id="divFeild">
            <input type="text" id="city" name="city"    onkeyup ="FunSelect()" /><br />
            <div id="wordDiv"> <ul id="myText"></ul></div>
        
        </div>
        <p>ssssssssssssssssssssssss</p>
        <p>ssssssssssssssssssssssss</p>
        <p>ssssssssssssssssssssssss</p>
    </form>
</body>
</html>
=======================================================================using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1
{
    /// <summary>
    /// Handler1 的摘要说明
    /// </summary>
    public class Handler1 : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            List<string> stringList = new List<string>() { 
               "湖北省","湖北省1","湖北省2","湖南省","河南省","河北省"
            };
            var word =context. Request.QueryString["word"];
            string result = "";
            foreach (string s in stringList)
            {
                if (s.Contains(word))
                {
                    //result +="<li style='list-style-type:none;' onclick='FunGetValue('"+s+"')'>"+ s+"</li>";
                    result += "<li style='list-style-type:none;' onclick='FunGetValue(this.innerText)'>" + s + "</li>";
                }
                // FunGetValue( '湖北省1')
            }
            context.Response.Write(result);
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
原文地址:https://www.cnblogs.com/blogLYF/p/5653997.html