這篇文章主要講解了“怎么用ajax實(shí)現(xiàn)簡單搜索功能”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么用ajax實(shí)現(xiàn)簡單搜索功能”吧!

在大石橋等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì) 網(wǎng)站設(shè)計(jì)制作定制網(wǎng)站設(shè)計(jì),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),成都全網(wǎng)營銷推廣,外貿(mào)營銷網(wǎng)站建設(shè),大石橋網(wǎng)站建設(shè)費(fèi)用合理。
本文實(shí)例講述了基于ajax的簡單搜索實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
這里使用兩個.aspx文件,一個叫Default.aspx,一個叫AjaxOperations.aspx,第一個用來輸入搜索數(shù)據(jù),后一個用來對搜索關(guān)鍵字進(jìn)行處理。js文件夾下面還有一個testJs.js的文件,它就是ajax操作的核心部分。不錯,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對象,以發(fā)送ajax請求
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ù)
}
//用來設(shè)置當(dāng)鼠標(biāo)失去焦點(diǎn)后文本框的隱藏
document.onmousedown = function() {
if (!event.srcElement.pop)
clearNames();
} //填寫輸入框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" />
<input type="submit" id="cmdSearch" name="cmdSearch" value="Search" title="Run Search" />
<div id="popup" >
<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"];
/* 測試用 實(shí)際項(xiàng)目中可以對數(shù)據(jù)庫進(jìn)行檢索等等相關(guā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)測試通過,復(fù)制粘貼運(yùn)行試試看吧。
感謝各位的閱讀,以上就是“怎么用ajax實(shí)現(xiàn)簡單搜索功能”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對怎么用ajax實(shí)現(xiàn)簡單搜索功能這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!
本文題目:怎么用ajax實(shí)現(xiàn)簡單搜索功能
文章位置:http://www.chinadenli.net/article28/pesocp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、外貿(mào)建站、微信小程序、面包屑導(dǎo)航、軟件開發(fā)、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)