婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁(yè) > 知識(shí)庫(kù) > 基于ajax的簡(jiǎn)單搜索實(shí)現(xiàn)方法

基于ajax的簡(jiǎn)單搜索實(shí)現(xiàn)方法

熱門(mén)標(biāo)簽:騰訊地圖標(biāo)注位置能用多久 個(gè)人怎樣在百度地圖標(biāo)注地名 硅語(yǔ)電話機(jī)器人公司 ai機(jī)器人電銷資源 超級(jí)大富翁地圖標(biāo)注 云呼外撥網(wǎng)絡(luò)電話系統(tǒng) 機(jī)器人電銷騙局揭秘 地圖標(biāo)注項(xiàng)目怎么樣 越南河內(nèi)地圖標(biāo)注

本文實(shí)例講述了基于ajax的簡(jiǎn)單搜索實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:

這里使用兩個(gè).aspx文件,一個(gè)叫Default.aspx,一個(gè)叫AjaxOperations.aspx,第一個(gè)用來(lái)輸入搜索數(shù)據(jù),后一個(gè)用來(lái)對(duì)搜索關(guān)鍵字進(jìn)行處理。js文件夾下面還有一個(gè)testJs.js的文件,它就是ajax操作的核心部分。不錯(cuò),code is cheap。看代碼:

testJs.js

// 此函數(shù)等價(jià)于document.getElementById /document.all
function $(s) { if (document.getElementById) { return eval('document.getElementById("' + s + '")'); } else { return eval('document.all.' + s); } }
// 創(chuàng)建 XMLHttpRequest對(duì)象,以發(fā)送ajax請(qǐng)求 
function createXMLHTTP() {
 var xmlHttp = false;
 var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0",
       "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP",
       "Microsoft.XMLHTTP"];
 for (var i = 0; i  arrSignatures.length; i++) {
  try {
   xmlHttp = new ActiveXObject(arrSignatures[i]);
   return xmlHttp;
  }
  catch (oError) {
   xmlHttp = false; //ignore
  }
 }
 // throw new Error("MSXML is not installed on your system."); 
 if (!xmlHttp  typeof XMLHttpRequest != 'undefined') {
  xmlHttp = new XMLHttpRequest();
 }
 return xmlHttp;
}
function addAjaxSearch() {
 inputField = $("txtSearch");
 completeTable = $("suggestTb");
 completeDiv = $("popup");
 completeBody = $("suggestBody");
 var tempStr = inputField.value;
 // alert(tempStr);
 var keyWord = encodeURI(tempStr);
 if (tempStr == "")
  return;
 var xmlReq = createXMLHTTP();
 xmlReq.open("post", "AjaxOperations.aspx?searchKeyword=" + keyWord, true);
 xmlReq.onreadystatechange = function() {
  if (xmlReq.readyState == 4) {
   if (xmlReq.status == 200) {
    //xmlReq.responseText為輸出的那段字符串
    setNames(xmlReq.responseText);
   }
   else {
    alert("Connect the server failed!");
   }
  }
 }
 xmlReq.send(null);
}
// 設(shè)置div中的表格數(shù)據(jù)
function setNames(names) {
 if (names == "") {
  clearNames();
  return;
 }
 clearNames(); // 清空div中已有的的表格數(shù)據(jù)
 setOffsets(); // 設(shè)置div到合適的位置
 var row, cell, txtNode;
 var s = names.split("#");
 for (var i = 0; i  s.length; i++) { // 顯示類似search下拉選擇項(xiàng)
  var nextNode = s[i];
  row = document.createElement("tr");
  cell = document.createElement("td");
  cell.onmouseout = function() { this.style.backgroundColor = ''; };
  cell.onmouseover = function() { this.style.backgroundColor = '#E8F2FE'; };
  cell.onclick = function() { completeField(this); }; // 搜索框設(shè)置為選擇的數(shù)據(jù)
  cell.pop = "T";
  txtNode = document.createTextNode(nextNode);
  cell.appendChild(txtNode);
  row.appendChild(cell);
  $("suggestBody").appendChild(row);
 }
}
// 清空div中已有的的表格數(shù)據(jù)
function clearNames() {
 completeBody = $("suggestBody");
 var ind = completeBody.childNodes.length;
 for (var i = ind - 1; i >= 0; i--) {
  completeBody.removeChild(completeBody.childNodes[i]);
 }
 completeDiv = $("popup");
 completeDiv.style.border = "none";
}
// 設(shè)置div到合適的位置
function setOffsets() {
 completeTable.style.width = inputField.offsetWidth; +"px";
 var left = calculateOffset(inputField, "offsetLeft");
 var top = calculateOffset(inputField, "offsetTop") + inputField.offsetHeight;
 completeDiv.style.border = "black 1px solid";
 completeDiv.style.left = left + "px";
 completeDiv.style.top = top + "px";
}
function calculateOffset(field, attr) {
 var offset = 0;
 while (field) {
  offset += field[attr];
  field = field.offsetParent;
 }
 return offset;
}
// 搜索框設(shè)置為選擇的數(shù)據(jù)
function completeField(cell) {
 inputField.value = cell.firstChild.nodeValue; // 搜索框設(shè)置為選擇的數(shù)據(jù)
 clearNames(); //清空div中已有的的表格數(shù)據(jù)
}
//用來(lái)設(shè)置當(dāng)鼠標(biāo)失去焦點(diǎn)后文本框的隱藏
document.onmousedown = function() {
 if (!event.srcElement.pop)
  clearNames();
} //填寫(xiě)輸入框

Default.aspx:

%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebTest2008.Default" %>
!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 id="Head1" runat="server">
 title>Ajax Search/title>
 script src="js/testJs.js" type="text/javascript">/script>
 style type="text/css" media="screen">
  body
  {
   font: 11px arial;
  }
  .suggest_link
  {
   background-color: #FFFFFF;
   padding: 2px 0px 2px 0px;
   border:solid 1px #cceeff;
  }
  .suggest_link_over
  {
   background-color: #E8F2FE;
   padding: 2px 0px 2px 0px;
  }
  #search_suggest
  {
   position: absolute;
   background-color: #FFFFFF;
   text-align: left;
   border: 1px solid #000000;
  }
 /style>
/head>
body>
 input name="txtSearch" id="txtSearch" type="text" class="suggest_link" onkeyup="addAjaxSearch();" maxlength="200" style="width: 200px" />nbsp;
 input type="submit" id="cmdSearch" name="cmdSearch" value="Search" title="Run Search" />
 div id="popup" style="position: absolute">
  table id="suggestTb" cellspacing="0" cellpadding="0" bgcolor="#fffafa" border="0">
   tbody id="suggestBody">
   /tbody>
  /table>
 /div>
/body>
/html>

Default.aspx.cs:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebTest2008
{
 public partial class Default : System.Web.UI.Page
 {
  protected void Page_Load(object sender, EventArgs e)
  {
  }
 }
}

AjaxOperations.aspx:

復(fù)制代碼 代碼如下:
%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxOperations.aspx.cs" Inherits="WebTest2008.AjaxOperations" %>

AjaxOperations.aspx.cs:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebTest2008
{
 public partial class AjaxOperations : System.Web.UI.Page
 {
  protected void Page_Load(object sender, EventArgs e)
  {
   if (!string.IsNullOrEmpty(Request["searchKeyword"]))
   {
    string tempStr = Request["searchKeyword"];
    /* 測(cè)試用 實(shí)際項(xiàng)目中可以對(duì)數(shù)據(jù)庫(kù)進(jìn)行檢索等等相關(guān)操作,這里簡(jiǎn)化了 */
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append(tempStr + " #");
    sb.Append("#");
    sb.Append(tempStr += " " + tempStr);
    sb.Append("#");
    sb.Append(tempStr += " " + tempStr);
    Response.Write(sb.ToString().TrimEnd(new char[] { '#' })); 
   }
  }
 }
}

上面的代碼我都已經(jīng)測(cè)試通過(guò),復(fù)制粘貼運(yùn)行試試看吧。

剛看到一篇文章里說(shuō),“實(shí)時(shí)搜索帶來(lái)的痛苦要遠(yuǎn)大于他帶來(lái)的幫助。這就是為什么Google Suggest還處于beta測(cè)試而并沒(méi)有放在主頁(yè)上的原因。在Start.com Live.com上搜索的時(shí)候你是不能使用返回按鈕來(lái)查看上一次搜索或返回上一頁(yè)的。或許還沒(méi)有人來(lái)完成這項(xiàng)工作,但是完成這個(gè)工作應(yīng)該是很困難的至少是不太明知的或者會(huì)因此帶來(lái)更多的麻煩。(譯注:現(xiàn)在已經(jīng)有很多開(kāi)源的框架可以實(shí)現(xiàn)歷史記錄功能)”。其實(shí)ajax實(shí)時(shí)搜索還是很有吸引力的,現(xiàn)在的很多網(wǎng)站都有這個(gè)功能。學(xué)習(xí)一下還是很有意義的。

希望本文所述對(duì)大家ajax程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • Ajax獲取數(shù)據(jù)然后顯示在頁(yè)面的實(shí)現(xiàn)方法
  • jsp頁(yè)面 列表 展示 ajax異步實(shí)現(xiàn)方法
  • 頁(yè)面向下滾動(dòng)ajax獲取數(shù)據(jù)的實(shí)現(xiàn)方法(兼容手機(jī))
  • yii2使用ajax返回json的實(shí)現(xiàn)方法
  • Ajax學(xué)習(xí)筆記---3種Ajax的實(shí)現(xiàn)方法【推薦】
  • 詳解PHP+AJAX無(wú)刷新分頁(yè)實(shí)現(xiàn)方法
  • JSP+jquery使用ajax方式調(diào)用json的實(shí)現(xiàn)方法
  • ThinkPHP通過(guò)AJAX返回JSON的兩種實(shí)現(xiàn)方法
  • jquery的ajax和getJson跨域獲取json數(shù)據(jù)的實(shí)現(xiàn)方法
  • ajax 三種實(shí)現(xiàn)方法實(shí)例代碼

標(biāo)簽:遼源 邢臺(tái) 洛陽(yáng) 鄭州 舟山 內(nèi)蒙古 林芝 海南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《基于ajax的簡(jiǎn)單搜索實(shí)現(xiàn)方法》,本文關(guān)鍵詞  基于,ajax,的,簡(jiǎn)單,搜索,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《基于ajax的簡(jiǎn)單搜索實(shí)現(xiàn)方法》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于基于ajax的簡(jiǎn)單搜索實(shí)現(xiàn)方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 乐平市| 林周县| 莱阳市| 武鸣县| 中山市| 宁强县| 东莞市| 丹棱县| 潜山县| 民丰县| 宜宾市| 开远市| 沙洋县| 镇巴县| 南乐县| 彰化市| 大渡口区| 衡水市| 济宁市| 大田县| 蓬安县| 长兴县| 泸水县| 玛纳斯县| 屏南县| 新泰市| 台安县| 中江县| 大竹县| 寿光市| 密云县| 阜新| 新田县| 沛县| 湟中县| 随州市| 东光县| 云霄县| 册亨县| 中阳县| 潞西市|