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

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

1. 创建一个html并 引入jQuery.js query.autocomplete.js 和 query.autocomplete.css
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 获取数据

<%@ 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”,如果需要传递中文,只需要
String key = new String(request.getParameter("q").getBytes("iso8859-1"),"utf-8");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值