jQueryui autocomplete使用樣本

來源:互聯網
上載者:User

標籤:style   blog   java   color   使用   os   檔案   io   

html標籤:

<input  id="province" name="province"></li><input  id="city" name="city"  type="text"></li>

 

需要引入的js檔案:

<script src="js/jquery.min.js"></script><script src="js/jquery-ui.min.js"></script>

頁面上的javascript代碼

<script>    var url = "<%=servlet.getPath() %>";    var lastXhr;    $("#province").autocomplete({        minLength: 1,        source: function (request, response) {            var term = request.term;            lastXhr = $.getJSON(url+"?beanName=ProvinceData", request, function (data, status, xhr) {                if (xhr === lastXhr) {                    response( $.grep( data, function( item ){                        if(item.indexOf(request.term)!=-1)return item;                    }) );                 }            });        },        select:function(event, ui){            $("#city").autocomplete({                minLength:1,                source:function(request1,response1){                    var term = request1.term;                    $.getJSON(url+"?beanName=CityData&province="+ui.item.value,request1,function(data1,status1,xhr1){                        response1($.grep(data1,function(item){                            if(item.indexOf(request1.term)!=-1)return item;                        }));                    });                }            });        }    });</script>

服務端的java代碼:

@BeanDef(value="ProvinceData",scope=PrototypeScope.class)    public static Object provinceData(@BeanRef ProvinceService provinceService) throws Exception{        List<Province> provinces = provinceService.getProvinceByName(new String(""));        List<String> nameList = new ArrayList<String>();        for(Province province:provinces){            nameList.add(province.getName());        }        JsonModel model = new JsonModel(nameList);        return model;    }        @BeanDef(value="CityData",scope=PrototypeScope.class)    public static Object cityData(@Param("province")String province,            @BeanRef ProvinceService provinceService,            @BeanRef CityService cityService) throws Exception{        System.out.println(province);        List<Province> provinces = provinceService.getProvinceByName(province);        List<String> nameList  = new ArrayList<String>();        if(provinces != null && provinces.size()==1){            List<City> cities = cityService.getCityByProvinceID(provinces.get(0).getId());            for(City city:cities){                nameList.add(city.getName());            }        }else{            nameList.add("");        }        JsonModel model = new JsonModel(nameList);        System.out.println(model.toString());        return model;    }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.