chosen實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)
本文實(shí)例為大家分享了chosen實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:

一、資源
1.1、css資源
<link href="../../css/plugins/chosen/chosen.css" 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="">請(qǐng)選擇省份</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="">請(qǐng)選擇城市</option>
</select>
</div>
</div>
<div class="form-group col-sm-2" style="margin-left: 36px;">
<div class="input-group">
<select data-placeholder="選擇區(qū)縣..." class="area-chosen-select" id="area" tabindex="3">
<option value="">請(qǐng)選擇區(qū)縣</option>
</select>
</div>
</div>
</div>
三、javascript代碼
<script type="text/javascript">
$(function(){
$('.province-chosen-select').chosen({
disable_search_threshold: 10,
no_results_text: '沒有找到',//沒有搜索到匹配項(xiàng)時(shí)顯示的文字
width: '240px',
disable_search:false, // 設(shè)置為 true 隱藏單選框的搜索框
disable_search_threshold:0 //少于 n 項(xiàng)時(shí)隱藏搜索框
});
$('.city-chosen-select').chosen({
disable_search_threshold: 10,
no_results_text: '沒有找到',//沒有搜索到匹配項(xiàng)時(shí)顯示的文字
width: '240px',
disable_search:false, // 設(shè)置為 true 隱藏單選框的搜索框
disable_search_threshold:0 //少于 n 項(xiàng)時(shí)隱藏搜索框
});
$('.area-chosen-select').chosen({
disable_search_threshold: 10,
no_results_text: '沒有找到',//沒有搜索到匹配項(xiàng)時(shí)顯示的文字
width: '240px',
disable_search:false, // 設(shè)置為 true 隱藏單選框的搜索框
disable_search_threshold:0 //少于 n 項(xiàng)時(shí)隱藏搜索框
});
})
//Chosen 觸發(fā)標(biāo)準(zhǔn)的 change 事件,同時(shí)會(huì)傳遞 selected or deselected 參數(shù), 方便用戶獲取改變的選項(xiàng)
$('.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 元素選項(xiàng)時(shí)應(yīng)該觸發(fā)此事件,以更新 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 元素選項(xiàng)時(shí)應(yīng)該觸發(fā)此事件,以更新 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: 根據(jù)省份獲取城市列表
* @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: 根據(jù)城市獲取區(qū)縣列表
* @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);
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
輕松學(xué)習(xí)jQuery插件EasyUI EasyUI創(chuàng)建CRUD應(yīng)用
這篇文章主要幫大家輕松學(xué)習(xí)jQuery插件EasyUI,并利用EasyUI創(chuàng)建CRUD應(yīng)用,感興趣的小伙伴們可以參考一下2015-11-11
jQuery實(shí)現(xiàn)單擊按鈕遮罩彈出對(duì)話框效果(1)
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)單擊按鈕遮罩彈出對(duì)話框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
jQuery使用JSONP實(shí)現(xiàn)跨域獲取數(shù)據(jù)的三種方法詳解
這篇文章主要介紹了jQuery使用JSONP實(shí)現(xiàn)跨域獲取數(shù)據(jù)的三種方法,結(jié)合實(shí)例形式對(duì)比分析了jsonp跨域獲取數(shù)據(jù)的3種常用操作技巧,需要的朋友可以參考下2017-05-05
jquery實(shí)現(xiàn)多條件篩選特效代碼分享
這篇文章主要介紹了jquery實(shí)現(xiàn)多條件篩選特效,推薦給大家,有需要的小伙伴可以參考下。2015-08-08
基于jQuery實(shí)現(xiàn)的扇形定時(shí)器附源碼下載
這篇文章給大家介紹基于jquery實(shí)現(xiàn)的扇形定時(shí)器,涉及到pietimer知識(shí)點(diǎn)的學(xué)習(xí),對(duì)jquery定時(shí)器感興趣的朋友一起了解了解吧2015-10-10
基于jQuery實(shí)現(xiàn)二級(jí)下拉菜單效果
這篇文章主要介紹了jQuery實(shí)現(xiàn)二級(jí)下拉菜單效果的相關(guān)資料,二級(jí)下拉菜單在實(shí)際應(yīng)用中非常的常見,比如企業(yè)網(wǎng)站的產(chǎn)品分類,或者部門分類等等,需要的朋友可以參考下2016-02-02
js根據(jù)json數(shù)據(jù)中的某一個(gè)屬性來給數(shù)據(jù)分組的方法
今天小編就為大家分享一篇js根據(jù)json數(shù)據(jù)中的某一個(gè)屬性來給數(shù)據(jù)分組的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10

