jquery输入框模糊匹配

备注说明:①index.jsp为主页面,提供输入

getdata.jsp为业务处理部分,可以替换为action或者servlet形式

DummyDB.java为数据匹配处理类

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'index.jsp' starting page</title>
    <link href="jquery.autocomplete.css" type=text/css rel=stylesheet>
    <script type="text/javascript" src="jquery.js"></script>
	<script type="text/javascript" src="jquery.autocomplete.js"></script>
	<style>  
         input {   
             font-size: 120%;   
        }   
    </style>  
  </head>
  
  <body>
     <h3>Country</h3>  
     <input type="text" id="country" name="country"/>  
  
.     <script>  
        $("#country").autocomplete("getdata.jsp");   
     </script> 
  </body>
</html>

 

 

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.hzp.DummyDB"%>
<%
	DummyDB db = new DummyDB();

	String query = request.getParameter("q");

	List<String> countries = db.getData(query);

	Iterator<String> iterator = countries.iterator();
	while (iterator.hasNext()) {
		String country = (String) iterator.next();
		out.println(country);
	}
%>

 

 

 

 

package com.hzp;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class DummyDB {
	private int totalCountries;
	private String data = "Afghanistan, Albania, bmw, Zimbabwe";
	private List<String> countries;

	public DummyDB() {
		countries = new ArrayList<String>();
		StringTokenizer st = new StringTokenizer(data, ",");

		while (st.hasMoreTokens()) {
			countries.add(st.nextToken().trim());
		}
		totalCountries = countries.size();
	}

	public List<String> getData(String query) {
		String country = null;
		query = query.toLowerCase();
		List<String> matched = new ArrayList<String>();
		for (int i = 0; i < totalCountries; i++) {
			country = countries.get(i).toLowerCase();
			if (country.startsWith(query)) {
				matched.add(countries.get(i));
			}
		}
		return matched;
	}
}
 

 

 

你可能感兴趣的:(JavaScript,java,jquery,jsp,css)