ASP.NET(C#)中的自動下拉框的實現

來源:互聯網
上載者:User
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > <html> <head> <title>Ajax實現自動提示的文字框</title> <style> <!-- body{      font-family:Arial, Helvetica, sans-serif;      font-size:12px; padding:0px; margin:5px; } form{padding:0px; margin:0px;} input{      /* 使用者輸入框的樣式 */      font-family:Arial, Helvetica, sans-serif;      font-size:12px; border:1px solid #000000;      width:200px; padding:1px; margin:0px; } #popup{      /* 提示框div塊的樣式 */      position:absolute; width:202px;      color:#004a7e; font-size:12px;      font-family:Arial, Helvetica, sans-serif;      left:41px; top:25px; } #popup.show{      /* 顯示提示框的邊框 */          border:1px solid #004a7e; } #popup.hide{      /* 隱藏提示框的邊框 */      border:none; } /* 提示框的樣式風格 */ ul{      list-style:none;      margin:0px; padding:0px; } li.mouseOver{      background-color:#004a7e;      color:#FFFFFF; } li.mouseOut{      background-color:#FFFFFF;      color:#004a7e; } --> </style> <script language= "javascript" > var oInputField;    //考慮到很多函數中都要使用 var oPopDiv;        //因此採用全域變數的形式 var oColorsUl; var xmlHttp; function createXMLHttpRequest(){      if (window.ActiveXObject)          xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );      else if (window.XMLHttpRequest)          xmlHttp = new XMLHttpRequest(); } function initVars(){      //初始設定變數      oInputField = document.forms[ "myForm1" ].colors;      oPopDiv = document.getElementById( "popup" );      oColorsUl = document.getElementById( "colors_ul" ); } function clearColors(){      //清除提示內容      for (var i=oColorsUl.childNodes.length-1;i>=0;i--)          oColorsUl.removeChild(oColorsUl.childNodes[i]);      oPopDiv.className = "hide" ; } function setColors(the_colors){      //顯示提示框,傳入的參數即為匹配出來的結果組成的數組      clearColors();    //每輸入一個字母就先清除原先的提示,再繼續      oPopDiv.className = "show" ;      var oLi;      for (var i=0;i<the_colors.length;i++){          //將匹配的提示結果逐一顯示給使用者          oLi = document.createElement( "li" );          oColorsUl.appendChild(oLi);          oLi.appendChild(document.createTextNode(the_colors[i]));             oLi.onmouseover = function(){              this .className = "mouseOver" ;    //滑鼠經過時高亮          }          oLi.onmouseout = function(){              this .className = "mouseOut" ;    //離開時恢複原樣          }          oLi.onclick = function(){              //使用者點擊某個匹配項時,設定輸入框為該項的值              oInputField.value = this .firstChild.nodeValue;              clearColors();    //同時清除提示框          }      } } function findColors(){      initVars();        //初始設定變數      if (oInputField.value.length > 0){          createXMLHttpRequest();        //將使用者輸入發送給伺服器          var sUrl = "9-10.aspx?sColor=" + oInputField.value + "&timestamp=" + new Date().getTime();          xmlHttp.open( "GET" ,sUrl, true );          xmlHttp.onreadystatechange = function(){              if (xmlHttp.readyState == 4 && xmlHttp.status == 200){                  var aResult = new Array();                  if (xmlHttp.responseText.length){                      aResult = xmlHttp.responseText.split( "," );                      setColors(aResult);    //顯示伺服器結果                  }                  else                      clearColors();              }          }          xmlHttp.send( null );      }              else          clearColors();    //無輸入時清除提示框(例如使用者按del鍵) } </script> </head> <body> <form method= "post" name= "myForm1" > Color: <input type= "text" name= "colors" id= "colors" onkeyup= "findColors();" /> </form> <div id= "popup" >      <ul id= "colors_ul" ></ul> </div> </body> </html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.