Jquery實現邊輸入邊查詢,仿百度,並可以選取查詢的值賦到輸入框

來源:互聯網
上載者:User

本例使用的是jquery-1.9.1,c#,稍加修改,也可以用於jsp,php等


主顯示頁面bsbc.aspx,代碼如下


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="bsbc.aspx.cs" Inherits="Test_bsbc" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title>    <script src="jquery-1.9.1.js" type="text/javascript"></script>    <script type="text/javascript">        $().ready(function() {            //輸入框鍵盤按鍵鬆開事件            $("#shuru").keyup(function() {                $.post("bsbcUser.aspx?username=" + $("#shuru")[0].value, function(data) {                    $("#testDIV").show();                    $("#testDIV").empty();                    $("#testDIV").append(data);                    //偶數行                    //$("tr:odd").css("background-color", "#e5e5e5");//背景色                    $("tr:odd").each(function() {                    });                    //奇數行                    $("tr:even").each(function() {                        //單擊事件                        $(this).click(function() {                        });                    });                });                /*****************/                //設定div位置,在input後面                var shuru = $("#shuru");                var offset = shuru.offset();                $("#testDIV").css('left', offset.left + shuru.width() + 2 + 'px')              .css('top', offset.top + 'px')              .css('position', 'absolute')              .css('z-index', '99')              .fadeIn();                /******************/                //Div滑鼠滑過事件                $("#testDIV").mouseover(                   function() {                       //firstCol列添加click事件,給其他控制項賦值                       $(".firstCol").click(function() {                           $("#shuru").val($(this).text());                           $("#testDIV").empty();                       });                   }                );                $("#testDIV").mouseleave(                   function() {                       //置空div                       $("#testDIV").empty();                   }                );                /******************/                //斷行符號事件                 //  $(window).keydown(function(event) {                    //   switch (event.keyCode) {                           // ...                            // 不同的按鍵可以做不同的事情                            // 不同的瀏覽器的keycode不同                            // 更多詳細資料:     http://unixpapa.com/js/key.html                                                 //case 13:                        //   break;                    //   }                 //  });                /*******************/            });        });    </script></head><body>    <form id="form1" runat="server">    </form>    <h3>        請在下面的輸入框中鍵入字母(A - Z):</h3>      <input id="shuru" type="text" style="border: 1px solid" />        <div id="testDIV" style="height: 200px; width: 200px; overflow: auto; color: #0000FF">    </div></body></html>

post跳回傳頁面,前台無代碼html代碼,後台代碼直接respon.wrilte,輸出html文本

bsbcUser.aspx前台代碼

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="bsbcUser.aspx.cs" Inherits="Test_bsbcUser" %><head id="Head1" runat="server"></head>

bsbcUser.aspx後台代碼


using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data;using System.IO;public partial class Test_bsbcUser : BasePageWithHR{    protected void Page_Load(object sender, EventArgs e)    {        Response.Write("<table  height='200px'>" + test() + "</table>");    }    public String test()    {        string sql = "select * from [user] where user_name like'%"+Request["username"]+"%'";        DataSet ds = Commons.PublicMethod.Instance.PublicTable(sql);//資料庫查詢        StringWriter sw = new StringWriter();                sw.WriteLine("<table cellspacing='0' style='background-color:#e5e5e5'border='1' id='tabTest'>");        if (ds.Tables[0].Rows.Count > 0)        {            int i = 0;            foreach (DataRow dr in ds.Tables[0].Rows)            {                i++;                sw.WriteLine("<tr>");                sw.WriteLine("<td>" + i);                sw.WriteLine("</td>");                sw.WriteLine("<td  class='firstCol'>" + dr["user_name"].ToString());                sw.WriteLine("</td>");                sw.WriteLine("<td>" + dr["password"].ToString());                sw.WriteLine("</td>");                sw.WriteLine("</tr>");            }        }        else {            sw.WriteLine("<tr>");            sw.WriteLine("<td>" );            sw.WriteLine("抱歉,沒有該關鍵詞的相關資料");            sw.WriteLine("</td>");            sw.WriteLine("</tr>");        }       sw.WriteLine("</table>");       return sw.ToString();    }}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.