AutoComplete控件就是在用户在文本框输入前几个字母或是汉字的时候,该控件就能从存放数据的文本或是数据库里将所有以这些字母开头的数据提示给用户,供用户选择,提供方便。
重要属性
1.TargetControlID:指定要实现提示功能的控件;
2.ServicePath:WebService的路径,提取数据的方法是写在一个WebService中的;
3.ServeiceMethod:写在WebService中的用于提取数据的方法的名字;
4.MinimumPrefixLength:用来设置用户输入多少字母才出现提示效果;
5.CompletionSetCount:设置提示数据的行数;
6.CompletionInterval:从服务器获取书的时间间隔,单位是毫秒。
二、利用jquery实现输入提示 首先我们依赖的js为
jquery-1.7.2.js
jquery.autocomplete.js
下面为输入框主页面index.jsp
- <!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="css/jquery.autocomplete.css" />
- <!-- 注释掉的部门只是为了更快的加载jquery.js,效果同<script src="js/jquery-1.7.2.js"></script> -->
- <!-- <script src="http://www.google.com/jsapi"></script> -->
- <!-- <script> -->
- <!-- google.load("jquery", "1.7.1"); -->
- <!-- </script> -->
- <script src="js/jquery-1.7.2.js"></script>
- <script src="js/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>
下面是负责与后台打交道的getdata.jsp
- <%@page import="java.util.Iterator"%>
- <%@page import="java.util.List"%>
- <%@page import="com.DummyDB"%>
- <%
- DummyDB db = new DummyDB();
- // query是从index传过来的已输入的参数
- String query = request.getParameter("q");
- // 根据后台代码及参数查询符合条件的参数,组成list
- List<String> countries = db.getData(query);
- // 迭代显示
- Iterator<String> iterator = countries.iterator();
- while(iterator.hasNext()) {
- String country = (String)iterator.next();
- out.println(country);
- }
- %>
下面是后台java对条件处理的方法类
三、对jquery.autocomplete.js的一点修改
- package com;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.StringTokenizer;
- public class DummyDB {
- private int totalCountries;
- private String data = "Afghanistan, Albania, Algeria, Andorra, Angola, Antigua & Deps,"+
- "Argentina, Armenia, Australia, Austria,Azerbaijan,Bahamas,Bahrain,Bangladesh,Barbados,"+
- "Belarus,Belgium,Belize,Benin,Bhutan,Bolivia,Bosnia Herzegovina,Botswana,Brazil,Brunei,"+
- "Bulgaria,Burkina,Burundi,Cambodia,Cameroon,Canada,Cape Verde,Central African Rep,Chad,"+
- "Chile,China,Colombia,Comoros,Congo,Congo {Democratic Rep},Costa Rica,Croatia,Cuba,Cyprus,"+
- "Czech Republic,Denmark,Djibouti,Dominica,Dominican Republic,East Timor,Ecuador,Egypt,El Salvador,"+
- "Equatorial Guinea, Eritrea,Estonia,Ethiopia,Fiji,Finland,France,Gabon,Gambia,Georgia,Germany,"+
- "Ghana, Greece, Grenada,Guatemala,Guinea,Guinea-Bissau,Guyana,Haiti,Honduras,Hungary,Iceland,"+
- "India, Indonesia,Iran,Iraq,Ireland {Republic},Israel,Italy,Ivory Coast,Jamaica,Japan,"+
- "Jordan,Kazakhstan,Kenya,Kiribati,Korea North,Korea South,Kosovo,Kuwait,Kyrgyzstan,Laos,"+
- "Latvia,Lebanon,Lesotho,Liberia,Libya,Liechtenstein,Lithuania,Luxembourg,Macedonia,Madagascar,"+
- "Malawi,Malaysia,Maldives,Mali,Malta,Marshall Islands,Mauritania,Mauritius,Mexico,Micronesia,"+
- "Moldova,Monaco,Mongolia,Montenegro,Morocco,Mozambique,Myanmar, {Burma},Namibia,Nauru,Nepal,"+
- "Netherlands,New Zealand,Nicaragua,Niger,Nigeria,Norway,Oman,Pakistan,Palau,Panama,Papua New Guinea,"+
- "Paraguay,Peru,Philippines,Poland,Portugal,Qatar,Romania,Russian Federation,Rwanda,St Kitts & Nevis,"+
- "St Lucia,Saint Vincent & the Grenadines,Samoa,San Marino,Sao Tome & Principe,Saudi Arabia,Senegal,"+
- "Serbia,Seychelles,Sierra Leone,Singapore,Slovakia,Slovenia,Solomon Islands,Somalia,South Africa,"+
- "Spain,Sri Lanka,Sudan,Suriname,Swaziland,Sweden,Switzerland,Syria,Taiwan,Tajikistan,Tanzania, "+
- "Thailand,Togo,Tonga,Trinidad & Tobago,Tunisia,Turkey,Turkmenistan,Tuvalu,Uganda,Ukraine,United Arab Emirates,"+
- "United Kingdom,United States,Uruguay,Uzbekistan,Vanuatu,Vatican City,Venezuela,Vietnam,Yemen,Zambia,Zimbabwe";
- private List<String> countries;
- /**
- *
- * @brief 构造函数,同时将data字符串转化为List
- *
- */
- public DummyDB() {
- countries = new ArrayList<String>();
- StringTokenizer st = new StringTokenizer(data, ",");
- System.out.println("DummyDB");
- while(st.hasMoreTokens()) {
- countries.add(st.nextToken().trim());
- }
- totalCountries = countries.size();
- }
- /**
- *
- * @brief 根据query条件查询出符合条件的List<String>
- *
- * @param query
- * @return
- */
- public List<String> getData(String query) {
- System.out.println("query:"+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();
- /*这边是采用的startsWith方法,表示开始为query的String
- 若换做contains方法则表示只要包含有query的就符合条件*/
- if(country.startsWith(query)) {
- matched.add(countries.get(i));
- }
- }
- return matched;
- }
- }
今天本来是要在一个输入框中输入多个用户名,然后每个用分号隔开,然后我就直接开始寻找jquery.autocomplete.js中传参的方法,终于让我给找着了:
首先,是我们输入的参数,这个参数首先会出现在onChange()方法中,
定义个全局变量用来存放已经确定的参数inputvalue = v.substring(0,v.lastIndexOf(";")+1);
然后我添加了这么一句:v = v.substring(v.lastIndexOf(";")+1);
这样,传到后面的查询条件就是最后一个分号后面的内容了
- function onChange() {
- // ignore if the following keys are pressed: [del] [shift] [capslock]
- if( lastKeyPressCode == 46 || (lastKeyPressCode > 8 && lastKeyPressCode < 32) ) return $results.hide();
- var v = $input.val();
- //给全局变量赋值
- inputvalue = v.substring(0,v.lastIndexOf(";")+1);
- v = v.substring(v.lastIndexOf(";")+1);
- if (v == prev) return;
- prev = v;
- if (v.length >= options.minChars) {
- $input.addClass(options.loadingClass);
- requestData(v);
- } else {
- $input.removeClass(options.loadingClass);
- $results.hide();
- }
- };
最后是返回给输入框的内容,即下面selectItem(li)方法,
其中那句$input.val(inputvalue+v);就是返回结果到输入框,我将全局变量inputvalue添加到选中的选项值v前面,这样,就能提示多个了
- function selectItem(li) {
- if (!li) {
- li = document.createElement("li");
- li.extra = [];
- li.selectValue = "";
- }
- var v = $.trim(li.selectValue ? li.selectValue : li.innerHTML);
- input.lastSelected = v;
- prev = v;
- // alert("selectItem:"+v);
- $results.html("");
- $input.val(inputvalue+v);
- hideResultsNow();
- if (options.onItemSelect) setTimeout(function() { options.onItemSelect(li) }, 1);
- };
还有前面getdata.jsp中out.println(country);要改为 out.println(country+";");
这样,每次提示的内容就会自动加上分号了。
不过,后来发现貌似jquery.autocomplete.js本身就支持输入多个,中间用特殊符号隔开的,只是暂时还未尝试,下回尝试过再分享!
附上修改过的jquery.autocomplete.js