用AJAX實作類別似GOOGLE搜尋方塊的功能

來源:互聯網
上載者:User

第一個頁面;

 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());

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.