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);
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery+.net實(shí)現(xiàn)瀏覽更多內(nèi)容(改編php版本)
改編自php版本這里記錄.net 下的實(shí)現(xiàn);首先創(chuàng)建數(shù)據(jù)庫表test,并插入一些測試數(shù)據(jù)接下來建立一個(gè)html文件,感興趣的朋友可以參考下哈,希望您可以幫助到你2013-03-03
基于jQuery滑動(dòng)桿實(shí)現(xiàn)購買日期選擇效果
這是一款基于jQuery的滑動(dòng)桿購買日期選擇插件,它的外觀仿的是阿里云的服務(wù)器購買日期選擇界面。這款jQuery插件非常適合在一些虛擬產(chǎn)品購買頁面上使用,它可以幫助你的用戶快速選擇產(chǎn)品的購買日期,感興趣的朋友跟著小編學(xué)習(xí)吧2015-09-09
從重置input file標(biāo)簽中看jQuery的 .val() 和 .attr(“value”) 區(qū)別
這篇文章主要介紹了從重置input file標(biāo)簽中看jQuery的 .val() 和 .attr(“value”) 區(qū)別 的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
在JavaScript中重寫jQuery對(duì)象的方法實(shí)例教程
這篇文章主要介紹了在JavaScript中重寫jQuery對(duì)象的方法,在某些情況下jQuery無法滿足應(yīng)用開發(fā)的時(shí)候可以采用javascript重寫jQuery方法來滿足功能的實(shí)現(xiàn),需要的朋友可以參考下2014-08-08
jQuery+css實(shí)現(xiàn)的時(shí)鐘效果(兼容各瀏覽器)
這篇文章主要介紹了jQuery+css實(shí)現(xiàn)的時(shí)鐘效果,使用js的setTimeout方法實(shí)時(shí)修改頁面元素,實(shí)現(xiàn)動(dòng)態(tài)顯示時(shí)鐘的功能.該代碼可兼容各瀏覽器,需要的朋友可以參考下2016-01-01
一個(gè)有意思的鼠標(biāo)點(diǎn)擊文字特效jquery代碼
這篇文章主要介紹了一個(gè)有意思的鼠標(biāo)點(diǎn)擊文字特效jquery代碼,需要的朋友可以參考下2017-09-09

