<Html>
<head>
<title>不刷新頁面查詢的方法</title>
<meta http-equiv="Content-Type" content="text/Html; charset=gb2312">
</head>
<script language="Javascript">
<!--初始化,將數據島中數據裝入列表框中-->
function loadinsel()
{
var employeeid,employeelastname; //分別存放雇員ID和雇員名字
root=document.all.XMLemployees.childNodes.item(0); //返回第一個元素--employee
for(i=0;i<root.childNodes.length;i++){
getnode=root.childNodes(i); //得到empolyee的一個子節點
employeeid=root.childNodes(i).getAttribute("emid");//得到雇員ID
for(j=0;j<getnode.childNodes.length;j++){
employeeinf=getnode.childNodes(j).nodeName;
if(employeeinf=="lastname"){
employeelastname=getnode.childNodes(j).text; //得到雇員名字
}
}
//將得到的employeeid和employeelastname寫進select中
if(employeeid!="" && employeelastname!=""){
option1=document.createElement("option");
option1.text=employeelastname;
option1.value=employeeid;
employeelist.add(option1);
}
}
}
<!--初始化,從數據島中檢索數據,裝入列表框中-->
function findemployee(){
var employeelastname,employeeid; //分別存放雇員名字和雇員ID
employeelastname="";
employeeid="";
findtext=window.findcontent.value; //得到檢索條件
//清除列表框
employeecount=employeelist.length
for(i=employeecount-1;i>=0;i--){
employeelist.remove(i);
}
root=window.XMLemployees.childNodes(0);
for(i=0;i<root.childNodes.length;i++){
getitem=root.childNodes(i); //得到empolyee的一個子節點
employeeid=root.childNodes(i).getAttribute("emid"); //得到雇員ID
for(j=0;j<getitem.childNodes.length;j++){
if(getitem.childNodes(j).nodeName=="lastname"){
employee_temp=getitem.childNodes(j).text;
if(employee_temp.indexOf(findtext)!=-1){ //查找匹配項
employeelastname=employee_temp; //找到名字匹配的雇員
}
}
}
//將符合條件的雇員信息寫進select中
if(employeeid!="" && employeelastname!=""){
option1=document.createElement("option");
option1.value=employeeid;
option1.text=employeelastname;
window.employeelist.add(option1);
employeeid="";
employeelastname="";
}
}
}
</script>
<body bgcolor="#FFFFFF" text="#000000" onload="Javascript:loadinsel()">
<table width="80%" border="1">
<tr>
<td> 請輸入查詢條件:
<input type="text" name="findcontent">
<input type="button" name="Submit" value="查找" onclick="Javascript:findemployee()">
</td>
</tr>
<tr>
<td> 查詢結果:
<select name="employeelist">
</select>
</td>
</tr>
</table>
<?XML version="1.0" encoding="gb2312"?>
<%
servername="wyb" '服務器名
user="sa" '用戶名
pw="" '用戶密碼
databasename="northwind" '數據庫名
set conn=server.CreateObject("adodb.connection")
conn.Open "DRIVER=SQL Server;SERVER="&servername&";UID="&user&";pwd="&pw&";DATABASE="&databasename
set rs=server.CreateObject("adodb.recordset")
sql="Select employeeid,lastname from employees order by employeeid"
rs.Open sql,conn%>
<!--將數據庫中信息放入數據島中-->
<xml id="XMLemployees">
<employee>
<%do while not rs.eof%>
<employeeitem emid="<%=rs("employeeid")%>">
<lastname><%=rs("lastname")%></lastname>
</employeeitem>
<%rs.movenext%>
<%loop%>
</employee> </XML>
<%rs.close
set rs=nothing
%>
</body>
</Html>