天天看點

chosen-省市區三級關聯

效果圖:

chosen-省市區三級關聯

一、資源

1.1、css資源

<link href="../../css/plugins/chosen/chosen.css" target="_blank" rel="external nofollow"  rel="stylesheet">
           

1.2、js資源

<script src="../../js/plugins/chosen/chosen.jquery.js"></script>
           

二、代碼

<div class="row">
                             <div class="form-group col-sm-2">
                                 <div class="input-group">
                                     <select data-placeholder="選擇省份..." id="province" class="province-chosen-select" tabindex="1">
                                         <option value="">請選擇省份</option>
                                         <#if provinceList?? && provinceList?size gt 0>
                                          <#list provinceList as province>
                                                    <option value="${province.provinceId!}" >${province.name!}</option>
                                          </#list>
                                         </#if>
                                     </select>
                                 </div>
                             </div>
                             <div class="form-group col-sm-2" style="margin-left: 36px;">
                                    <div class="input-group">
                                        <select data-placeholder="選擇城市..." id="city" class="city-chosen-select" tabindex="2">
                                            <option value="">請選擇城市</option>
                                        </select>
                                    </div>
                             </div>
                             <div class="form-group col-sm-2" style="margin-left: 36px;">
                                    <div class="input-group">
                                        <select data-placeholder="選擇區縣..." class="area-chosen-select" id="area" tabindex="3">
                                            <option value="">請選擇區縣</option>
                                        </select>
                                    </div>
                             </div>
                     </div>
           

三、javascript代碼

<script type="text/javascript">
     $(function(){
           $('.province-chosen-select').chosen({
                  disable_search_threshold: 10,
                  no_results_text: '沒有找到',//沒有搜尋到比對項時顯示的文字
                  width: '240px',
                  disable_search:false, // 設定為 true 隐藏單選框的搜尋框
                  disable_search_threshold:0 //少于 n 項時隐藏搜尋框
                });
           $('.city-chosen-select').chosen({
                  disable_search_threshold: 10,
                  no_results_text: '沒有找到',//沒有搜尋到比對項時顯示的文字
                  width: '240px',
                  disable_search:false, // 設定為 true 隐藏單選框的搜尋框
                  disable_search_threshold:0 //少于 n 項時隐藏搜尋框
                });
           $('.area-chosen-select').chosen({
                  disable_search_threshold: 10,
                  no_results_text: '沒有找到',//沒有搜尋到比對項時顯示的文字
                  width: '240px',
                  disable_search:false, // 設定為 true 隐藏單選框的搜尋框
                  disable_search_threshold:0 //少于 n 項時隐藏搜尋框
                });
           
     })
     //Chosen 觸發标準的 change 事件,同時會傳遞 selected or deselected 參數, 友善使用者擷取改變的選項
     $('.province-chosen-select').on('change', function(e, params) {
           findCitiesByProvince(e, params);
           });
     $('.city-chosen-select').on('change', function(e, params) {
           findAreasByCity(e, params);
           });
     
     function findCitiesByProvince(e, params) {
           var provinceId = params.selected;
           $.post("/common/find_cities_by_province", {
                "provinceId":provinceId
                }, function(data){
                     $('#city option:first').nextAll().remove();
                     $('#area option:first').nextAll().remove();
                     var html = '';
                    for (var i = 0; i < data.length; i++) {
                     html+='<option value="'+data[i].cityId+'" hassubinfo="true">'+data[i].name+'</option>'
                    }
                      $("#city").append(html);
                       //通過 JS 改變 select 元素選項時應該觸發此事件,以更新 Chosen 生成的選框
                       $('.city-chosen-select').trigger('chosen:updated');
                       $('.area-chosen-select').trigger('chosen:updated');
                })
     }
     function findAreasByCity(e, params) {
           var cityId = params.selected;
           $.post("/common/find_areas_by_city", {
                "cityId":cityId
                }, function(data){
                     $('#area option:first').nextAll().remove();
                     var html = '';
                    for (var i = 0; i < data.length; i++) {
                     html+='<option value="'+data[i].areaId+'" hassubinfo="true">'+data[i].name+'</option>'
                    }
                       $("#area").append(html);
                        //通過 JS 改變 select 元素選項時應該觸發此事件,以更新 Chosen 生成的選框
                        $('.area-chosen-select').trigger('chosen:updated');
                })
     }
     function submitBtn() {
           $("#result_div").html('');
           var provinceId = $("#province").val();
           var provinceName = $("#province option:selected").text();
           var cityId = $("#city").val();
           var cityName = $("#city option:selected").text();
           var areaId = $("#area").val();
           var areaName = $("#area option:selected").text();
          $("#result_div").append("provinceId="+provinceId+"<br>")
                .append("provinceName="+provinceName+"<br>")
                .append("cityId="+cityId+"<br>")
                .append("cityName="+cityName+"<br>")
                .append("areaId="+areaId+"<br>")
                .append("areaName="+areaName+"<br>");
     }
    </script>
           

四、java代碼

/**
      *
      * @Title: findCitiesByProvince
      * @Description: 根據省份擷取城市清單
      * @author: 大都督
      * @param provinceId
      * @return
      * @return: MessageInfo
      */
     @RequestMapping("/find_cities_by_province")
     @ResponseBody
     public List<City> findCitiesByProvince(String provinceId) {
           Assert.hasText(provinceId, StringText.provinceId_must);
           return cityDao.findByProvinceId(provinceId);
     }
    /**
      *
      * @Title: findAreasByCity
      * @Description: 根據城市擷取區縣清單
      * @author: 大都督
      * @param cityId
      * @return
      * @return: List<City>
      */
     @RequestMapping("/find_areas_by_city")
     @ResponseBody
     public List<Area> findAreasByCity(String cityId) {
           Assert.hasText(cityId, StringText.cityId_must);
           return areaDao.findByCity(cityId);
     }