跟google你输入关键字,动态显示大概条数的提示,首先看下效果
第一步css:
- #dSuggest {
- z-index:0;
- position:absolute;
- background-color:#ffffff;
- }
- #sug {
- list-style:none;
- border:solid 1px #09f;
- margin-left:0;
- margin-top:0;
- width:310px;
- }
- #sug li {
- width:100%;
- padding-left:2px;
- }
- #sug span {
- position:absolute;
- z-index:200;
- right:3px;
- }
- .select {
- background-color:#09f;
- color:#ffffff;
- cursor:default;
- }
#dSuggest {
z-index:0;
position:absolute;
background-color:#ffffff;
}
#sug {
list-style:none;
border:solid 1px #09f;
margin-left:0;
margin-top:0;
width:310px;
}
#sug li {
width:100%;
padding-left:2px;
}
#sug span {
position:absolute;
z-index:200;
right:3px;
}
.select {
background-color:#09f;
color:#ffffff;
cursor:default;
}
第二步javascript:
- var XMLHttp=false;
- var j=-1;
- function CreateXMLHttpRequest() {
- if(window.ActiveXObject)
- {
- XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
- }
- else if(window.XMLHttpRequest)
- {
- XMLHttp = new XMLHttpRequest();
- }
- if(!XMLHttp && typeof XMLHttpRequest != 'undefined') {
- XMLHttp = new XMLHttpRequest();
- }
- }
- function GetSuggest(keyword,e) {
- var keyc=GetKeyCode(e);
- if(keyc==40 || keyc==38 || keyc==13)
- return;
- var _url="Search.ashx?keyword="+escape(keyword);
- CreateXMLHttpRequest();
- XMLHttp.onreadystatechange=function(){
- if(XMLHttp.readystate==4) {
- if(XMLHttp.status==200) {
- $("dSuggest").innerHTML=XMLHttp.responseText;
- j=-1;
- }
- }
- }
- XMLHttp.open("get",_url,true);
- XMLHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
- XMLHttp.send(null);
- }
- function set_style(num) {//设置样式
- for(var i=0;i< $$().length;i++) {
- if(i>=0 && i< $$().length&&i==num) {
- $$()[i].className="select";
- }
- else
- {
- $$()[i].className="";
- }
- }
- }
- function mo(nodevalue) {
- j=nodevalue;
- set_style(j);
- }
- function form_submit() {
- var lis=$$();
- if(lis==null)
- return;
- for(var i=0;i
- S.LENGTH;I++) key='lis[i].innerHTML.substring(0,lis[i].innerHTML.indexOf("<SPAN' var { if(lis[i].className='="select")'>"));
- $('ctl00_ContentPlaceHolder1_txtKeyword').value=key;
- $("dSuggest").innerHTML="";
- }
- }
- form_submits();
- }
- function form_submits() {
- var stainfo = $("ctl00_ContentPlaceHolder1_stainfo").value;
- var pno = $("ctl00_ContentPlaceHolder1_txtKeyword").value;
- if(stainfo == "in")
- {
- window.location = "inStock.aspx?sta=in&pno="+pno;
- }
- else
- {
- window.location = "inStock.aspx?pno="+pno;
- }
- }
- function cleanBox()
- {
- $("dSuggest").innerHTML="";
- }
- function updown(e) {
- var keyc=GetKeyCode(e);
- if(keyc==40 || keyc==38) {
- if($$()==null)
- return;
- if(keyc==40) {
- j++;
- if(j>=$$().length)
- {
- j=0;
- }
- } else if(keyc==38) {
- j--;
- if(j==-1)
- j=$$().length-1;
- }
- set_style(j);
- }
- }
- function GetKeyCode(e) {//取得不同浏览器下的键盘事件值
- var keyc;
- if(window.event) {//ie键盘事件
- keyc=e.keyCode;
- } else if (e.which) {//火狐
- keyc=e.which;
- }
- return keyc;
- }
- function $(id) {
- return document.getElementById(id);
- }
- function $$() {
- var ul=$("sug");
- if(ul==null)
- return null;
- return ul.getElementsByTagName("li");
- }
- //getPos(this,'Left'),getPos(this,'Top')+this.offsetHeight
- function getPos(el,sProp) {
- var iPos = 0;
- while (el!=null) {
- iPos+=el["offset" + sProp];
- el = el.offsetParent;
- }
- return iPos;
- }
- function setPosition(obj) {
- $('dSuggest').style.left=getPos(obj,'Left');
- $('dSuggest').style.top=getPos(obj,'Top')+obj.offsetHeight
- }
var XMLHttp=false;
var j=-1;
function CreateXMLHttpRequest() {
if(window.ActiveXObject)
{
XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
XMLHttp = new XMLHttpRequest();
}
if(!XMLHttp && typeof XMLHttpRequest != 'undefined') {
XMLHttp = new XMLHttpRequest();
}
}
function GetSuggest(keyword,e) {
var keyc=GetKeyCode(e);
if(keyc==40 || keyc==38 || keyc==13)
return;
var _url="Search.ashx?keyword="+escape(keyword);
CreateXMLHttpRequest();
XMLHttp.onreadystatechange=function(){
if(XMLHttp.readystate==4) {
if(XMLHttp.status==200) {
$("dSuggest").innerHTML=XMLHttp.responseText;
j=-1;
}
}
}
XMLHttp.open("get",_url,true);
XMLHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
XMLHttp.send(null);
}
function set_style(num) {//设置样式
for(var i=0;i< $$().length;i++) {
if(i>=0 && i< $$().length&&i==num) {
$$()[i].className="select";
}
else
{
$$()[i].className="";
}
}
}
function mo(nodevalue) {
j=nodevalue;
set_style(j);
}
function form_submit() {
var lis=$$();
if(lis==null)
return;
for(var i=0;i
"));
$('ctl00_ContentPlaceHolder1_txtKeyword').value=key;
$("dSuggest").innerHTML="";
}
}
form_submits();
}
function form_submits() {
var stainfo = $("ctl00_ContentPlaceHolder1_stainfo").value;
var pno = $("ctl00_ContentPlaceHolder1_txtKeyword").value;
if(stainfo == "in")
{
window.location = "inStock.aspx?sta=in&pno="+pno;
}
else
{
window.location = "inStock.aspx?pno="+pno;
}
}
function cleanBox()
{
$("dSuggest").innerHTML="";
}
function updown(e) {
var keyc=GetKeyCode(e);
if(keyc==40 || keyc==38) {
if($$()==null)
return;
if(keyc==40) {
j++;
if(j>=$$().length)
{
j=0;
}
} else if(keyc==38) {
j--;
if(j==-1)
j=$$().length-1;
}
set_style(j);
}
}
function GetKeyCode(e) {//取得不同浏览器下的键盘事件值
var keyc;
if(window.event) {//ie键盘事件
keyc=e.keyCode;
} else if (e.which) {//火狐
keyc=e.which;
}
return keyc;
}
function $(id) {
return document.getElementById(id);
}
function $$() {
var ul=$("sug");
if(ul==null)
return null;
return ul.getElementsByTagName("li");
}
//getPos(this,'Left'),getPos(this,'Top')+this.offsetHeight
function getPos(el,sProp) {
var iPos = 0;
while (el!=null) {
iPos+=el["offset" + sProp];
el = el.offsetParent;
}
return iPos;
}
function setPosition(obj) {
$('dSuggest').style.left=getPos(obj,'Left');
$('dSuggest').style.top=getPos(obj,'Top')+obj.offsetHeight
}
第三步:ASPX页面设置
- <FORM id=form1 onkeypress="if(GetKeyCode(event)==13){return false;}" runat="server" 标签>
- <INPUT style="WIDTH: 310px" onkeydown=if(GetKeyCode(event)==13)form_submit(); id=txtKeyword onkeyup=GetSuggest(this.value,event);updown(event) runat="server" autocomplete="off">
- <DIV id=dSuggest></DIV>
- </FORM>
第四步:创建文件search.ashx
- < %@ WebHandler Language="C#" Class="Search" %>
- using System;
- using System.Web;
- using System.Data.SqlClient;
- public class Search : IHttpHandler {
- public void ProcessRequest (HttpContext context) {
- context.Response.ContentType = "text/plain";
- string sKeyword = context.Request["keyword"];
- if (string.IsNullOrEmpty(sKeyword))
- {
- context.Response.End();
- }
- SqlConnection conn = new SqlConnection("Server=(local);User ID=sa;Password=123;DataBase=CMStock");
- SqlCommand cmd = new SqlCommand("select top 20 * from Product where pno like '" + sKeyword + "%' OR name like '%" + sKeyword + "%';", conn);
- conn.Open();
- SqlDataReader dr = cmd.ExecuteReader();
- if (dr.HasRows)
- {
- context.Response.Write("
- <UL id=sug>");
- int i = 0;
- while (dr.Read())
- {
- string pno = dr["pno"].ToString();
- if (pno.Length > 15)
- {
- pno = pno.Substring(0, 15) + "...";
- }
- string pname = dr["name"].ToString();
- if (pname.Length > 30)
- {
- pname = pname.Substring(0, 30) + "...";
- }
- context.Response.Write(string.Format("
- <LI οnmοuseοver='\"mo(this.value)\"' οnclick='\"form_submit()\"' value={0}>{1}<SPAN>{2}</SPAN>
- ", i, pno, pname));
- i++;
- }
- context.Response.Write("</LI></UL>
- ");
- }
- else
- {
- context.Response.Write("
- <UL id=sug>");
- context.Response.Write(string.Format("
- <LI οnmοuseοver='\"mo(this.value)\"' οnclick='\"form_submit()\"' value={0}>{1}<SPAN>{2}</SPAN>
- ", 0, sKeyword, "该商品货号货名称错误,请更换!"));
- context.Response.Write("</LI></UL>
- ");
- }
- conn.Close();
- dr.Close();
- cmd.Dispose();
- }
- public bool IsReusable {
- get {
- return false;
- }
- }
- }
< %@ WebHandler Language="C#" Class="Search" %> using System; using System.Web; using System.Data.SqlClient; public class Search : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; string sKeyword = context.Request["keyword"]; if (string.IsNullOrEmpty(sKeyword)) { context.Response.End(); } SqlConnection conn = new SqlConnection("Server=(local);User ID=sa;Password=123;DataBase=CMStock"); SqlCommand cmd = new SqlCommand("select top 20 * from Product where pno like '" + sKeyword + "%' OR name like '%" + sKeyword + "%';", conn); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { context.Response.Write("
- "); int i = 0; while (dr.Read()) { string pno = dr["pno"].ToString(); if (pno.Length > 15) { pno = pno.Substring(0, 15) + "..."; } string pname = dr["name"].ToString(); if (pname.Length > 30) { pname = pname.Substring(0, 30) + "..."; } context.Response.Write(string.Format("
- {1}{2} ", i, pno, pname)); i++; } context.Response.Write("
- "); context.Response.Write(string.Format("
- {1}{2} ", 0, sKeyword, "该商品货号货名称错误,请更换!")); context.Response.Write("
运行 收工!