转载:jQuery + ajax仿google下拉列表提示功能

jQuery + ajax仿google下拉列表提示功能

文章分类:Web前端

根据客户的需求,在某些输入框要实现类似Google的autocomplete。JQuery早就给我们提供了现成的实现。并且应用很简单。只要最后展示的样式,可以通过调整Css来实现。

1. 创建一个html并 引入jQuery.js  query.autocomplete.js 和 query.autocomplete.css
html代码:
Java代码 复制代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"       
  2.   "http://www.w3.org/TR/html4/loose.dtd">      
  3.    <html>      
  4.     <head>      
  5.       <link rel="stylesheet" type="text/css" href="jquery.autocomplete.css" />      
  6.        <script src="jquery.js"></script>      
  7.     <script src="jquery.autocomplete.js"></script>      
  8.     <style>      
  9.          input {       
  10.              font-size: 120%;       
  11.         }       
  12.     </style>      
  13. </head>      
  14.  <body>      
  15.    <h3>Country</h3>      
  16.      <input type="text" id="pid" name="pid"/>      
  17.       
  18. .     <script>      
  19.         $("#pid").autocomplete("getdata.jsp",{   
  20.                                      delay:10,   
  21.                                     minChars:1,   
  22.                                     matchSubset:1,   
  23.                                     matchContains:1,   
  24.                                     cacheLength:10,   
  25.                                     matchContains: true,      
  26.                                     scrollHeight: 250,    
  27.                                     width:250,   
  28.                                     autoFill:false  
  29.                                 });   
  30.      </script>      
  31.  </body>      
  32.  </html>        
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"    
  "http://www.w3.org/TR/html4/loose.dtd">   
   <html>   
    <head>   
      <link rel="stylesheet" type="text/css" href="jquery.autocomplete.css" />   
       <script src="jquery.js"></script>   
    <script src="jquery.autocomplete.js"></script>   
    <style>   
         input {    
             font-size: 120%;    
        }    
    </style>   
</head>   
 <body>   
   <h3>Country</h3>   
     <input type="text" id="pid" name="pid"/>   
   
.     <script>   
        $("#pid").autocomplete("getdata.jsp",{
						             delay:10,
						            minChars:1,
						            matchSubset:1,
						            matchContains:1,
						            cacheLength:10,
						            matchContains: true,   
						            scrollHeight: 250, 
						            width:250,
						            autoFill:false
						        });
     </script>   
 </body>   
 </html>      



2. 创建 getdata.jsp 获取数据

Java代码 复制代码
  1. <%@ page language="java" contentType="text/html; charset=GBK"  
  2.     pageEncoding="GBK"%>   
  3. <%@page import="java.util.List"%>   
  4. <%@page import="com.lccert.crm.quotation.Quotation"%>   
  5. <%@page import="com.lccert.crm.quotation.QuotationAction"%>   
  6.   
  7. <%   
  8.     String key = new String(request.getParameter("q").getBytes("iso8859-1"),"utf-8");   
  9.     List<Quotation> lists = QuotationAction.getInstance().searchQuotation(key,"part");   
  10.     for(int i=0;i<lists.size();i++) {   
  11.         Quotation quotation = lists.get(i);   
  12.         out.println(quotation.getPid());   
  13.     }   
  14. %>  
<%@ page language="java" contentType="text/html; charset=GBK"
	pageEncoding="GBK"%>
<%@page import="java.util.List"%>
<%@page import="com.lccert.crm.quotation.Quotation"%>
<%@page import="com.lccert.crm.quotation.QuotationAction"%>

<%
	String key = new String(request.getParameter("q").getBytes("iso8859-1"),"utf-8");
	List<Quotation> lists = QuotationAction.getInstance().searchQuotation(key,"part");
	for(int i=0;i<lists.size();i++) {
		Quotation quotation = lists.get(i);
		out.println(quotation.getPid());
	}
%>



3、注意后台JSP页面:jQuery通过ajax传递的参数名是“q”,如果需要传递中文,只需要
Java代码 复制代码
  1. String key = new String(request.getParameter("q").getBytes("iso8859-1"),"utf-8");  
String key = new String(request.getParameter("q").getBytes("iso8859-1"),"utf-8");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值