Chinaunix首页 | 论坛 | 博客
  • 博客访问: 396575
  • 博文数量: 70
  • 博客积分: 1919
  • 博客等级: 上尉
  • 技术积分: 1179
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 20:05
文章分类

全部博文(70)

文章存档

2014年(2)

2013年(29)

2012年(20)

2011年(1)

2010年(13)

2009年(5)

分类: Html/Css

2013-10-11 11:42:44

改代码实现ajax得到结果并且显示出结果,如截图:

点击(此处)折叠或打开

  1. <html>
  2. <title>ajax autocomplete</title>
  3. <head>    
  4.     <style type="text/css" media="screen">
  5.     .onmouset_out{
  6.         background-color: #99CCFF;
  7.         padding: 2px 6px 2px 6px;
  8.     }
  9.     .onmouset_over{
  10.         background-color: #006600;
  11.         padding: 2px 6px 2px 6px;
  12.     }
  13.     #result_display{
  14.         border: 1px solid #FFFFFF;
  15.     }
  16.     </style>
  17.     
  18.     <script type="text/javascript">
  19.         var xmlHttp;
  20.         function createXMLHttp(){
  21.             if(window.XMLHttpRequest){
  22.                 xmlHttp = new XMLHttpRequest();
  23.             }
  24.             else{
  25.                 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  26.             }
  27.         }
  28.         function ajaxSearch(){
  29.             var txtValue = document.getElementById("tags").value;
  30.             if(txtValue.length<2){
  31.                 return ;
  32.             }
  33.             if(Trim(txtValue) == ""){
  34.                 document.getElementById('result_display').style.visibility = "hidden";
  35.                 return;
  36.             }
  37.             createXMLHttp();
  38.         //    var url = "/servlet/searchhylx&zymc=" + encodeURI(encodeURI(txtValue));
  39.             var url = "/servlet/searchZylx";
  40.             xmlHttp.open("POST", url, true);
  41.             xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  42.             xmlHttp.onreadystatechange = _handle;
  43.             xmlHttp.send("zymc="+txtValue);
  44.         }
  45.         function _handle(){
  46.             if(xmlHttp.readyState == 4){
  47.                 if(xmlHttp.status == 200){
  48.                     var str = xmlHttp.responseText;
  49.                     //str的返回值其实形式为:["aa","bb","cc"]
  50.                     str=eval(str);
  51.                     var s = document.getElementById('result_display');
  52.                     s.innerHTML = '';
  53.                 }
  54.                 if(str.length > 0){
  55.                     document.getElementById('result_display').style.visibility = "visible";
  56.                 }
  57.                 for(i = 0; i < str.length; i++){
  58.                     var suggest = '
    ;
  59.                     suggest += 'onmouseout="onmouseOut(this);"';
  60.                     suggest += 'onclick="setSuggestValue(this.innerHTML);"';
  61.                     suggest += 'class="onmouset_out">' + str[i] + '
';
  •                     s.innerHTML += suggest;
  •                 }
  •             }
  •         }
  •         function Trim(str){
  •             str += "";
  •             while( (str.charAt(0)==' ')||(str.charAt(0)==' ')||(escape(str.charAt(0))=='%u3000') )
  •                  str=str.substring(1,str.length);
  •             while( (str.charAt(str.length-1)==' ')||(str.charAt(str.length-1)==' ')||(escape(str.charAt(str.length-1))=='%u3000') )
  •                 str=str.substring(0,str.length-1);
  •             return str;
  •         }
  •         function onmouseOver(div) {
  •             div.className = 'onmouse_over';
  •         }
  •         function onmousetOut(div) {
  •             div.className = 'onmouset_out';
  •         }
  •         function setSuggestValue(value) {
  •             document.getElementById('tags').value = value;
  •             document.getElementById('result_display').innerHTML = '';
  •             document.getElementById('result_display').style.display="none";
  •         }
  •     </script>
  •   </head>
  •   <body>
  • <div class="ui-widget">
  •     <label for="tags">Tags: </label>
  •     <input id="tags" onkeyup="ajaxSearch()">
  •     <div id="result_display" style="width:200px;visibility:hidden">
  • </div>
  •   </body>
  • </html>

  •  

    阅读(2911) | 评论(1) | 转发(0) |
    给主人留下些什么吧!~~

    codepearl2013-10-12 15:45:27

    效率如何呢