성 시내 3 급 연동 실현
효과 그림:
자원
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=""> </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>
3.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>
자바 코드
/**
*
* @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);
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.