jquery+webservice方式的百度搜索下拉提示

default.aspx

<%@ Page Language="C#"  AutoEventWireup="true" CodeFile="default.aspx.cs" Inherits="default_" EnableViewState="false" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>百度搜索下拉</title>
<script type="text/javascript" src="js/Jquery.js"></script>
</head>
<style>
#h_list {
background:#EEE;
z-index:99;
position:absolute;
font-size:12px;
}
#h_list li{
list-style-type:none;
font-size: 12px;
margin:0 0 0 0;
padding-left:2px;
}

#h_list .oubg {
height: 20px;
margin-top:1px;
text-align: left;
line-height:20px;
cursor:pointer;

}
#h_list .ovbg {
height: 20px;
margin-top:1px;
text-align: left;
line-height:20px;
cursor:pointer;
background:#DDD;
 }
</style>
<body>
<div id="h_list" style="left:313px; top:57px; "></div>
<form runat="server">
<asp:TextBox ID="TextBox1" runat="server" CssClass="ipt_1" Text="" MaxLength="50"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="" CssClass="btn_1" OnClick="Button1_Click"></asp:Button>
<script src="js/KeyDropDownList.js"></script>
</form>
</body>
</html>

QueryKey.cs

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;

/// <summary>
/// QueryKey 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class QueryKey : System.Web.Services.WebService {

    public QueryKey () {

        //如果使用设计的组件,请取消注释以下行
        //InitializeComponent();
    }
    [WebMethod]
    public string GetTitleByKeyWord(string keyword)
    {
        if (keyword == "")
        {
            return "";
        }
        string re = "";
        using (DataSet ds = SqlHelper.DBClass.getDataSet("Select Top 10 * From t_keyword Where keyword like '%" + keyword.Replace("'", "''") + "%' order by hits desc", "tb"))
        {
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow dr = ds.Tables[0].Rows[i];
                re += "<li οnclick=\"setval(this.innerHTML);\" οnmοuseοver=\"overBg(this)\" οnmοuseοut=\"outBg(this)\" class=\"oubg\">" + dr["keyword"].ToString() + "</li>";
            }
        }
        if (re == "") re = "暂无内容";
        return re;
    }
    [WebMethod]
    public void InsertKeyWord(string keyword)
    {
        using (DataSet ds = SqlHelper.DBClass.getDataSet("select * From t_keyword Where keyword='" + keyword.Replace("'", "''") + "'", "tb"))
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                SqlHelper.DBClass.ExecSQL("update t_keyword set hits=hits+1 where keyword='" + keyword.Replace("'", "''") + "'");
            }
            else
                SqlHelper.DBClass.ExecSQL("insert into t_keyword(keyword) values('" + keyword.Replace("'", "''") + "')");
        }
    }
   
}

js/KeyDropDownList.js

// JScript 文件
var CurrentObj;
function KeyDownList(){
 $("#h_list").width($(".ipt_1").width()+3);
 $("#h_list").css({"top":$(".ipt_1").offset().top+$(".ipt_1").height()+5,"left":$(".ipt_1").offset().left});
 if($(".ipt_1").val().replace(/(\s*$)/g, "")!=""){
        $.ajax({
            type:"POST",
            url:"QueryKey.asmx/GetTitleByKeyWord",
            data:"keyword="+$(".ipt_1").val(),
            success:function(re){
    $("#h_list").html(re.text);
    $("#h_list").show();
            }
        });
 }
 else{
  $("#h_list").hide();
//  $("#zindexDiv").hide();
 }
}
function setval(val){
 $(".ipt_1").val(val);
 $("#h_list").hide();
// $("#zindexDiv").hide();
}
function outBg(obj)
{
 obj.className="oubg";
}
function overBg(obj)
{
    CurrentObj = obj;
 obj.className="ovbg";
}
function MoveFocus(index){
    if(CurrentObj){
        $(CurrentObj).mouseout();
        var pre_index = $("#h_list").find("li").index(CurrentObj)+index;
        if(pre_index>=$("#h_list").find("li").size()){
            pre_index = 0;
        }
        else if(pre_index < 0){
            pre_index = $("#h_list").find("li").size()-1;
        }
        $("#h_list").find("li").eq(pre_index).mouseover();
    }
    else
        $("#h_list").find("li").eq(0).mouseover();
}
//下拉框
$(".ipt_1").bind("keyup",function(){
    if (event.keyCode == 40) {//down
        MoveFocus(1);  
    }  
    else if (event.keyCode == 38) {//up
        MoveFocus(-1);  
    }  
    else if (event.keyCode == 13) {//回车
if($("#h_list").css("display")=="none" || $("#h_list").html()=="暂无内容" || ($("#h_list li").size()==1 && $(".ipt_1").val()==$("#h_list li").html()))
            $(".btn_1").click();
        else if(CurrentObj)
            $(CurrentObj).click();
    }
    else
     KeyDownList();
});
$(".ipt_1").bind("focus",function(){
 $('.tishi').hide();
 if($(this).val()!="" && $(this).val()!="%")
  KeyDownList();
});
//搜索按钮
$(".btn_1").bind("click",function(){
    if($('.ipt_1').val()=="" || $('.ipt_1').val()=="%"){
        $('.ipt_1').val("");
        $(".tishi").show();
        return false;
    }
    else{
        $(".tishi").hide();
    }
    if($(".ipt_1").val().replace(/(\s*$)/g, "")!=""){
        $.ajax({
            type:"POST",
            url:"QueryKey.asmx/InsertKeyWord",
            data:"keyword="+$(".ipt_1").val(),
            async:false,
            success:function(result){
                return true;
            }
        });
    }
});

web.config

<?xml version="1.0"?>
<!--
    注意: 除了手动编辑此文件以外,您还可以使用
    Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
     “网站”->“Asp.Net 配置”选项。
    设置和注释的完整列表在
    machine.config.comments 中,该文件通常位于
    \Windows\Microsoft.Net\Framework\v2.x\Config 中
-->
<configuration>
 <appSettings>
  <add key="dbtype" value="sqlserver"/>
  <add key="oledbstr" value=""/>
  <add key="sqlstr" value="user id=sa;password=sa;initial catalog=db;data source=."/>
 </appSettings>
 <connectionStrings/>

 

<!--没有此处设置,webservice不能远程访问 -->
 <system.web>
  <webServices>
   <protocols>
    <add name="HttpPost"/>
    <add name="HttpGet"/>
   </protocols>
  </webServices> 


  <customErrors mode="Off"></customErrors>
  <pages validateRequest="false" enableViewStateMac="false" enableEventValidation="false" viewStateEncryptionMode="Never">
  </pages>
  <!--
            设置 compilation debug="true" 将调试符号插入
            已编译的页面中。但由于这会
            影响性能,因此只在开发过程中将此值
            设置为 true。
        -->
  <compilation debug="true">
  </compilation>
  <!--
            通过 <authentication> 节可以配置 ASP.NET 使用的
            安全身份验证模式,
            以标识传入的用户。
        -->
  <authentication mode="Windows"/>
  <!--
            如果在执行请求的过程中出现未处理的错误,
            则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
            开发人员通过该节可以配置
            要显示的 html 错误页
            以代替错误堆栈跟踪。

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
 </system.web>
</configuration>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值