一個AJax文本框輸入提示的例子,用ASP實現:
前台文件
- <!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>
- <meta http-equiv="Content-Type" content="text/Html; charset=utf-8" />
- <title>文本框輸入提示</title>
- <style type="text/CSS">...
- <!--
- .keyWord {...}{width:150px; height:20px; border:#0066FF 1px solid;}/**//*文本框樣式*/
- #keytishi {...}{width:150px; height:auto; border:#0066FF 1px solid; position:absolute; display:none;}/**//*提示層樣式*/
- #keytishi ul {...}{ margin:0;}/**//*提示層樣式*/
- #keytishi ul li{...}{margin:0;list-style-type:none; line-height:16px; height:16px; font-size:12px; padding:2px;}/**//*提示層樣式*/
- #keytishi ul li a {...}{display:block; width:150px; height:16px; text-decoration:none;}/**//*提示層樣式*/
- #keytishi ul li a:hover {...}{background-color:#0099FF;}/**//*提示層樣式*/
- -->
- </style>
- <script type="text/Javascript">...
- <!--
- //建立XMLHttpRequest對象
- var XMLhttp;
- try...{
- XMLhttp= new ActiveXObject('Msxml2.XMLHTTP');
- }catch(e)...{
- try...{
- XMLhttp= new ActiveXObject('Microsoft.XMLHTTP');
- }catch(e)...{
- try...{
- XMLhttp= new XMLHttpRequest();
- }catch(e)...{}
- }
- }
- function getKeyWord()...{
- var obj = document.getElementById("search");//獲取文本域對象
- if(obj.value=="")...{
- return;
- }
- var top=0;
- var left=0;
- while(obj)...{//此循環得到文件域對象在頁面中的絕對位置
- top += obj["offsetTop"];
- left += obj["offsetLeft"];
- objobj = obj.offsetParent;
- }
- XMLhttp.open("get","input.ASP?keyWord="+document.getElementById("search").value,true);
- XMLhttp.onreadystatechange = function()...{
- if(XMLhttp.readyState == 4)
- ...{
- if(XMLhttp.status == 200)
- ...{
- if(XMLhttp.responseText!="")...{
- document.getElementById("keytishi").innerHtml = unescape(XMLhttp.responseText);//把後台返回的數據填充到提示層
- document.getElementById("keytishi").style.left = left + "px";//設置提示層的位置,左
- document.getElementById("keytishi").style.top = (top + 25) + "px";//設置提示層的位置,上
- document.getElementById("keytishi").style.display = "block";//設置提示層可見
- }else...{
- document.getElementById("keytishi").innerHtml = "";//清空提示層
- document.getElementById("keytishi").style.display = "none";//設置提示層不可見
- }
- }
- else...{
- }
- }
- }
- XMLhttp.setRequestHeader("If-ModifIEd-Since","0");
- XMLhttp.send(null);
- }
- function input(str)...{
- document.getElementById("search").value=str;//從提示層選擇你需要的數據填充到文本框
- document.getElementById("keytishi").innerHtml = "";//清空提示層
- document.getElementById("keytishi").style.display = "none";//設置提示層不可見
- }
- //-->
- </script>
- </head>
- <body>
- <input type="text" class="keyWord" id="search" name="search" onkeyup="getKeyWord();" onclick="getKeyWord();" />
- <div id="keytishi"></div><!--提示層-->
- </body>
- </Html>
後台文件 [input.ASP]
- <%...@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
- <!--#include file="conn.ASP"-->
- <%...
- dim rs
- dim sql
- dim keyWords
- keyWrods = Request("keyWord")
- Set rs = Server.CreateObject("ADODB.Recordset")
- sql = "select * from king_test where keyWord like '%"&keyWrods&"%'"
- rs.open sql,conn,1,1
- if not (rs.bof and rs.eof) then
- Response.Write("<ul>")
- do while not rs.eof
- %>
- <li><a href="Javascript:void(null);" onclick="input('<%Response.Write(escape(rs("keyWord")))%>');"><%...Response.Write(escape(rs("keyWord")))%></a></li>
- <%...
- rs.movenext
- loop
- Response.Write("<ul>")
- end if
- rs.close
- set rs = nothing
- conn.close
- Set conn = nothing
- %>
escape與unescape是用來編碼的和解碼的,這是為了避免漢字出現亂碼,在XP + IE6,7,Firefox測試通過