第一個頁面;
function getHTTPObject()
{
var waystation=null;
if(window.ActiveXObject)
{
waystation=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
waystation=new XMLHttpRequest();
}
else
{
waystation=false;
}
return waystation;
}
function addCitys()
{
inputField = document.getElementById("textfield");
completeTable = document.getElementById("table1");
completeDiv = document.getElementById("popup");
completeBody = document.getElementById("body1");
var s=document.all.item("textfield").value
var aa=encodeURI(s);
if(s=="")
return;
var xmlrequest=getHTTPObject();
xmlrequest.open("post","getCity.aspx?id="+aa);
xmlrequest.onreadystatechange=function()
{
if(xmlrequest.readyState==4)
{
//xmlrequest.responseText為你AddCity中輸出的那段字串;
setNames(xmlrequest.responseText);
}
}
xmlrequest.send(null);
}
/產生與輸入內容匹配行
function setNames(names)
{
if(names=="")
{
clearNames();
return;
}
clearNames();
setOffsets();
var row, cell, txtNode;
var s=names.split("$");
for (var i = 0; i < s.length; i++)
{
//s[i]為td的
var nextNode =s[i];
row = document.createElement("tr");
cell = document.createElement("td");
cell.onmouseout = function() {this.style.backgroundColor='';};
cell.onmouseover = function() {this.style.backgroundColor='#ffff00';};
cell.onclick = function() { completeField(this); } ;
cell.pop="T";
txtNode = document.createTextNode(nextNode);
cell.appendChild(txtNode);
row.appendChild(cell);
document.getElementById("body1").appendChild(row);
}
}
//用來設定當滑鼠失去焦點後文字框的隱藏
document.onmousedown=function()
{
if(!event.srcElement.pop)
clearNames();
}//填寫輸入框
function completeField(cell)
{
inputField.value = cell.firstChild.nodeValue;
clearNames();
}
//清除自動完成行
function clearNames()
{
completeBody = document.getElementById("body1");
var ind = completeBody.childNodes.length;
for (var i = ind - 1; i >= 0 ; i--)
{
completeBody.removeChild(completeBody.childNodes[i]);
}
completeDiv = document.getElementById("popup");
completeDiv.style.border = "none";
}
//設定顯示位置
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;
}
<html>
<body>
<input name="textfield" id="textfield" type="text" class="input1" onkeyup="addCitys();" maxlength="20" style="width: 68px" /><div id="popup" style="POSITION: absolute">
<table id="table1" cellspacing="0" cellpadding="0" bgcolor="#fffafa" border="0" >
<tbody id="body1" >
</tbody>
</table>
</div>
</body>
</html
第二個頁面(getCity.aspx)
注意:必須刪除這個頁面中的所有的HTML
然後在cs檔案中做自己想要的處理,作為字串輸入;
getCity.cs
string id = Request.QueryString["id"].ToString();
SqlConnection con = new SqlConnection(sqlstr);
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from airport where EnName like '%" + id + "%' or CnName like '%"+id+"%'", con);
DataSet ds = new DataSet();
da.Fill(ds);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
sb.Append(ds.Tables[0].Rows[i][2].ToString() + " "+ds.Tables[0].Rows[i][1].ToString() + "$");
}
Response.Write(sb.ToString());