新的起點(diǎn) 新的開始

          快樂生活 !

          類似Gogole autocomplete 最簡單的實(shí)現(xiàn) ---- Jquery

              根據(jù)客戶的需求,在某些輸入框要實(shí)現(xiàn)類似Google的autocomplete。JQuery早就給我們提供了現(xiàn)成的實(shí)現(xiàn)。并且應(yīng)用很簡單。只要最后展示的樣式,可以通過調(diào)整Css來實(shí)現(xiàn)。

          1. 創(chuàng)建一個Index.jsp 引入jQuery  query.autocomplete.js 和 query.autocomplete.css。
           <!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" />
                 
          <script src="js/jquery-1.3.1.min.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>  

          2. 創(chuàng)建 getdata.jsp 獲取數(shù)據(jù)
            <%@page import="java.util.Iterator"%>
           
          <%@page import="java.util.List"%>
          <%@page import="net.blogjava.vincent.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);
               }

           
          %>  


          3. 創(chuàng)建net.blogjava.vincent.DummyDB 模仿從數(shù)據(jù)源獲取數(shù)據(jù)

          /*
           * To change this template, choose Tools | Templates
           * and open the template in the editor.
           
          */


          package net.blogjava.vincent;

          import java.util.ArrayList;
          import java.util.List;
          import java.util.StringTokenizer;

          /**
           *
           * 
          @author Administrator
           
          */

          public class DummyDB {
               
          private int totalCountries;
               
          private String data = "Afghanistan, Albania, 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;
               }

          }

           Ok  Run it. See the fllowing screenshot:

          目錄結(jié)構(gòu)如下:



          posted on 2009-07-10 23:16 advincenting 閱讀(2116) 評論(3)  編輯  收藏

          評論

          # re: 類似Gogole autocomplete 最簡單的實(shí)現(xiàn) ---- Jquery 2009-07-11 18:49 99網(wǎng)上書店

          看到了!有很大用處~~  回復(fù)  更多評論   

          # re: 類似Gogole autocomplete 最簡單的實(shí)現(xiàn) ---- Jquery[未登錄] 2009-07-17 11:06 jerry

          LZ您好,
          在我的eclipse中,
          <script>
          $("#country").autocomplete("getdata.jsp");
          </script>
          中的“¥”總是提示錯誤“$ is not defined”
          請問如何解決?   回復(fù)  更多評論   

          # re: 類似Gogole autocomplete 最簡單的實(shí)現(xiàn) ---- Jquery[未登錄] 2009-08-19 11:21 Prince

          樓上的換成jQuery  回復(fù)  更多評論   


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           

          公告

          Locations of visitors to this pageBlogJava
        1. 首頁
        2. 新隨筆
        3. 聯(lián)系
        4. 聚合
        5. 管理
        6. <2009年7月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          統(tǒng)計(jì)

          常用鏈接

          留言簿(13)

          隨筆分類(71)

          隨筆檔案(179)

          文章檔案(13)

          新聞分類

          IT人的英語學(xué)習(xí)網(wǎng)站

          JAVA站點(diǎn)

          優(yōu)秀個人博客鏈接

          官網(wǎng)學(xué)習(xí)站點(diǎn)

          生活工作站點(diǎn)

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 玛曲县| 嘉黎县| 梁河县| 石河子市| 自贡市| 茶陵县| 南溪县| 遵义县| 垦利县| 庆安县| 视频| 屯门区| 绩溪县| 弥渡县| 萨迦县| 浦城县| 多伦县| 礼泉县| 黑龙江省| 衡水市| 萨迦县| 探索| 婺源县| 申扎县| 万安县| 宜宾市| 英超| 育儿| 武乡县| 墨江| 凤庆县| 长白| 永靖县| 雷波县| 合水县| 黔西县| 富裕县| 肃北| 衡阳市| 武邑县| 正阳县|