Ajax和Json实现自动补全
1、index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link type="text/css" href="css/jquery-ui-1.10.4.custom.css" rel="stylesheet" />
<script src="js/jquery-1.10.2.js"></script>
<script src="js/jquery-ui-1.10.4.custom.js"></script>
<script type="text/javascript" >
$(function(){
//自动补全
$("#username").autocomplete({
minLength:1,
source: function(request,response){
$.ajax({
url: 'Test', // 后台请求路径
//请求参数
data:{
username:request.term//请求参数.换成$("#username").val()一样
},
//data为返回数据(json)
success: function( data ) {//回调函数
var d = JSON.parse(data);//将 JSON 字符串转换为对象
response(d);//响应
}
});
}
}); });
</script> </head> <body>
用户名:<input type="text" name="username" id="username" />
</body>
</html>
2、json的必须包,jquery-ui-1.10.4.custom.css,jquery-ui-1.10.4.custom.js
3、Test.java
package com.cn.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; public class Test extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String username = new String(request.getParameter("username").getBytes("iso8859-1"),"utf-8");
response.setContentType("text/html;charset=utf-8"); //看成数据库的数据
List list = new ArrayList();
list.add("jack");
list.add("tom");
list.add("toy");
list.add("json");
list.add("lily");
list.add("lucy"); //看成模糊匹配数据库返回的集合
List li = new ArrayList(); for(int i=0;i<list.size();i++){
if(list.get(i).toString().indexOf(username)!=-1){
li.add(list.get(i));
}
} //将list转json
JSONArray json = JSONArray.fromObject(li); PrintWriter out = response.getWriter(); out.print(json);
out.flush();
out.close();
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { this.doGet(request, response);
} }
Ajax和Json实现自动补全的更多相关文章
- jquery的输入框自动补全功能+ajax
jquery的输入框自动补全功能+ajax 2017年05月10日 18:51:39 辣姐什么鬼 阅读数:1461 标签: web前端 更多 个人分类: web前端 内容参考网友文章写成,原博的链 ...
- AJAX实现类似百度的搜索提示,自动补全和键盘、鼠标操作
<script type="text/javascript"> $(document).ready(function(){ var highlightIndex = - ...
- Autocomplete 自动补全(Webform实战篇)
开篇语 因为项目中需要用到一个自动补全的功能,功能描述: 需求一:新增收件人的时候,自动下拉显示出数据库中所有的收件人信息(显示的信息包括:姓名-收件地址-联系方式) 需求二:选中一个值得时候,分别赋 ...
- autocomplete实现联想输入,自动补全
jQuery.AutoComplete是一个基于jQuery的自动补全插件.借助于jQuery优秀的跨浏览器特性,可以兼容Chrome/IE/Firefox/Opera/Safari等多种浏览器. 特 ...
- bigautocomplete实现联想输入,自动补全
bigautocomplete是一款Jquery插件.用它实现仿搜索引擎文本框自动补全插件功能很实用,使用也很简单,引入了插件之后写几行代码就可以实现,可以灵活设置. 先看效果图: 上图是通过ajax ...
- jquery.autocomplete自动补全功能
项目实例: 一:js //SupplierAutoComplete.js $().ready(function () { $("#txtSupplier").autocomplet ...
- jquery 自动补全控件(支持IE6)待整理
自动补全控件(兼容IE6):http://bassistance.de/ download地址:http://jquery.bassistance.de/autocomplete/jquery.aut ...
- bootstrap3-typeahead 自动补全
很酷的一个自动补全插件 http://twitter.github.io/typeahead.js 在bootstrap中使用typeahead插件,完成自动补全 相关的文档:https://gith ...
- 练习笔记:net,JqueryUI实现自动补全功能
1.首先建立个空的Web项目 2.将下载好的JqueryUI文件保存到JS文件加下 3.引入JS文件 <link href="JS/css/ui-lightness/jquery-ui ...
随机推荐
- Luogu P5290 / LOJ3052 【[十二省联考2019]春节十二响】
联考Day2T2...多亏有这题...让我水了85精准翻盘进了A队... 题目大意: 挺简单的就不说了吧...(这怎么简述啊) 题目思路: 看到题的时候想了半天,不知道怎么搞.把样例画到演草纸上之后又 ...
- bzoj3702/bzoj2212 二叉树 (线段树合并)
用线段树记每个子树中包含的数,然后合并的时候算出来逆序对的数量(合并a,b时,就是size[ch[a][1]]*size[ch[b][0]]),来决定这个子树要不要翻转 #include<bit ...
- 为什么每次app访问服务器都建立新连接 导致服务器大量连接疯涨
运维发现服务器有大量连接不释放,而且每次app访问都会建立新连接. netstat -antlp |grep ESTAB|grep 8080|wc -l (访问服务器8080端口的已建立的连接数 ...
- spring代码异常捕获到logback logging.config=logback-spring.xml文件中不能输出异常e.printStackTrace
在spring中使用logging.config=logback-spring.xml将日志转存到了文件中.但是代码中的捕获的异常无法用 e.printStackTrace 打印到文件中.使用如下方法 ...
- bzoj3467: Crash和陶陶的游戏
就一篇题解: BZOJ3467 : Crash和陶陶的游戏 - weixin_34248487的博客 - CSDN博客 1.离线,建出Atrie树:B树的倍增哈希数组,节点按照到根路径字典序排序 2. ...
- 腾讯云centos7安装MySQL
centos就centos呗,为什么要加个腾讯云呢?有这种疑问的兄dei,一定是没被不同云的系统坑过啊,阿里云的Ubuntu和腾讯云的Ubuntu不一样,centos好像也有差别,各个云平台,同样的系 ...
- k短路(A*)
http://poj.org/problem?id=2449 #include <cstdio> #include <cstdlib> #include <cstring ...
- 认识EasyUI——DataGrid的onClickRow事件
关键代码: $("#dg2").datagrid({ onClickRow: function (index, row) { //easyui封装好的时间(被单机行的索引,被单击行 ...
- Game HDU - 3389 (博弈论)
Bob and Alice are playing a new game. There are n boxes which have been numbered from 1 to n. Each b ...
- TensorFlow install
$sudo pip install virtualenv Create a virtual environment (recommended) Create a new virtual environ ...