Suggest效果


  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  2. <html>  
  3.    <head>  
  4.      <title>AJAX</title>  
  5.      <meta http-equiv="pragma" content="no-cache">  
  6.      <meta http-equiv="cache-control" content="no-cache">  
  7.      <meta http-equiv="expires" content="0">      
  8.      <style type="text/css" media="screen">  
  9.       .onmouset_out {  
  10.         font-family:Arial, Helvetica, sans-serif;
  11.         font-size:12px;
  12.         font-color:#000000;
  13.         background-color: #ffffff;  
  14.         padding: 2px 6px 2px 4px;  
  15.         cursor:pointer;
  16.       }  
  17.       .onmouset_over {  
  18.         font-family:Arial, Helvetica, sans-serif; 
  19.         font-size:12px;
  20.         font-color:#000000;
  21.         background-color: #3366cc;  
  22.         padding: 2px 6px 2px 4px;  
  23.         cursor:pointer;
  24.       }  
  25.       #result_display {  
  26.         display:none;
  27.         width:200px;
  28.         height:240px;
  29.         overflow-y:auto;
  30.         border:1px solid #000000; 
  31.         position:absolute;
  32.         
  33.       }    
  34.      </style>  
  35.      <script type="text/javascript">  
  36.        var xmlHttp;   
  37.        
  38.        function createXmlHttp() {  
  39.         
  40.          if (window.XMLHttpRequest) {  
  41.             
  42.             xmlHttp = new XMLHttpRequest();   
  43.          } else {  
  44.            
  45.             xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  
  46.          }  
  47.        }  
  48.        var handle_name="";
  49.        var targetPos;
  50.        function inputSuggest(obj) {  
  51.           var txtValue;
  52.           var url;
  53.           targetPos=obj;
  54.           if(obj.id=="firstname"){         
  55.              txtValuedocument.getElementById(obj.id).value; 
  56.              url = "ajax.jsp?f=" + txtValue+"&suggest=1";  
  57.              handle_name="handle_firstname";
  58.           }
  59.           if(obj.id=="lastname"){
  60.              txtValuedocument.getElementById(obj.id).value; 
  61.              url = "ajax.jsp?l=" + txtValue+"&suggest=1"; 
  62.              handle_name="handle_lastname";
  63.           } 
  64.           createXmlHttp();
  65.           xmlHttp.onreadystatechange = _handle;  
  66.           xmlHttp.open("GET",url,true);  
  67.           xmlHttp.send(null);  
  68.           url=null;
  69.        }  
  70.     function CPos(x, y)
  71.     {
  72.         this.x = x;
  73.         this.y = y;
  74.     }
  75.     function GetObjPos(ATarget)
  76.     {
  77.         var target = ATarget;
  78.         var pos = new CPos(target.offsetLeft, target.offsetTop); 
  79.         var targettarget = target.offsetParent;
  80.         while (target)
  81.         {
  82.             pos.x += target.offsetLeft;
  83.             pos.y += target.offsetTop;
  84.             targettarget = target.offsetParent
  85.         }
  86.         return pos;
  87.     }
  88.        function _handle() {  
  89.           if(xmlHttp.readyState==4){  
  90.              if(xmlHttp.status==200){  
  91.                  var showDiv=document.getElementById('result_display');
  92.                  showDiv.innerHTML = xmlHttp.responseText; 
  93.                  //set position
  94.                  showDiv.style.left=GetObjPos(targetPos)["x"];
  95.                  showDiv.style.top=GetObjPos(targetPos)["y"]+targetPos.offsetHeight;
  96.                  showDiv.style.display="block";
  97.              }  
  98.           }  
  99.        }  
  100.        function setSuggestValue(value,handle) {  
  101.           if(handle=="handle_firstname"){
  102.               document.getElementById('firstname').value = value;  
  103.           }
  104.           if(handle=="handle_lastname"){
  105.               document.getElementById('lastname').value = value;  
  106.           }
  107.           document.getElementById('result_display').innerHTML = ''
  108.           document.getElementById('result_display').style.display="none";
  109.           handle_name="";
  110.        }  
  111.      </script>  
  112.    </head>  
  113.      
  114.    <body>  
  115.      <h3>this is a suggest example:</h3>  
  116.       <form id="frmSearch" action="">  
  117.         <input type="text" id="firstname" name="firstname" alt="input" onkeyup="inputSuggest(this);" style="width:200px"/>
  118.         <br> 
  119.         <input type="text" id="lastname" name="lastname" alt="input" onkeyup="inputSuggest(this);" style="width:200px"/>  
  120.         <input type="submit" id="search" value="search" alt="search"/><br />  
  121.         <div id="result_display">  
  122.         </div>  
  123.       </form>  
  124.    </body>  
  125.  </html>  
ajax.jsp片段

  1. <%@page contentType="text/html; charset=UTF-8" import="java.sql.*"%> 
  2. <%@page import="org.springframework.context.ApplicationContext"%>
  3. <%@page import="org.springframework.context.support.ClassPathXmlApplicationContext"%>
  4. <%@page import="com.sysium.openadaptor.hibernate.beans.SearchPerson"%>
  5. <html> 
  6. <body> 
  7. <%
  8.     String sql_firstname="";
  9.     String sql_lastname="";
  10.     String suggest="";
  11.     if(request.getParameter("f")!=null){
  12.         sql_firstname = (String) request.getParameter("f");
  13.     }
  14.     if(request.getParameter("l")!=null){
  15.         sql_lastname = (String) request.getParameter("l");
  16.     }
  17.     if(request.getParameter("suggest")!=null){
  18.         suggest = (String) request.getParameter("suggest");
  19.     }
  20.     String str = "";
  21.     if (suggest.equals("1")) {
  22.         if (sql_firstname != "") {
  23.             str = "select firstname from person where firstname like'%"
  24.             + sql_firstname + "%'";
  25.         }
  26.         if (sql_lastname != "") {
  27.             str = "select lastname from person where lastname like'%"
  28.             + sql_lastname + "%'";
  29.         }
  30.     } else {
  31.         if(sql_firstname != "" && sql_lastname != ""){
  32.             str = "select pid,firstname,lastname from person where firstname like'%"
  33.                 + sql_firstname + "%' and lastname like '%"
  34.                 + sql_lastname + "%'";
  35.         }else{
  36.             str = "select pid,firstname,lastname from person where firstname like'%"
  37.                 + sql_firstname + "%' or lastname like '%"
  38.                 + sql_lastname + "%'";
  39.         }
  40.     }
  41.     //String str1="select * from customers"
  42.     ApplicationContext ctx = new ClassPathXmlApplicationContext(
  43.             "../beans-config.xml");
  44.     SearchPerson bean = (SearchPerson) ctx.getBean("searchPerson");
  45.     Connection con = bean.getDataSource().getConnection();
  46.     Statement stmt = con.createStatement();
  47.     ResultSet rst = stmt.executeQuery(str);
  48.  %> 
  49.  <%
  50.  if (suggest.equals("1")){
  51.      while (rst.next()) {
  52.           if(sql_firstname!=""){
  53. %>
  54. <div class="onmouset_out" onmouseover="this.className='onmouset_over'" onmouseout="this.className='onmouset_out'" onclick="setSuggestValue(this.innerHTML,handle_name)">
  55. <%=rst.getString("firstname")%>
  56. </div> 
  57. <%            
  58.           }
  59.           if(sql_lastname!=""){
  60. %>
  61. <div class="onmouset_out" onmouseover="this.className='onmouset_over'" onmouseout="this.className='onmouset_out'" onclick="setSuggestValue(this.innerHTML,handle_name)">
  62. <%=rst.getString("lastname")%>
  63. </div> 
  64. <%
  65.           }
  66.     }
  67.         //關閉連接、釋放資源 
  68.         rst.close();
  69.         stmt.close();
  70.         con.close();
  71.  }else{
  72.  %>
  73. <select id="personinfo" name="personinfo" multiple="multiple" style="width:466px" size="18">
  74. <%
  75. while (rst.next()) {
  76. %> 
  77. <option value="<%=rst.getString("pid")%>"><%=rst.getString("pid")%> <%=rst.getString("firstname")%> <%=rst.getString("lastname")%></option>
  78. <%
  79.     }
  80.     //關閉連接、釋放資源 
  81.     rst.close();
  82.     stmt.close();
  83.     con.close();
  84. %> 
  85. </select>
  86. <%
  87.  }
  88. %>
  89. </body> 
  90. </html>




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章