码迷,mamicode.com
首页 > 编程语言 > 详细

Spring MVC+JSP实现三级联动

时间:2015-11-25 21:11:57      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

jsp代码

<script type="text/javascript">
    $(function() {
        initProvinces();
    });
    /**
     * 获取省列表
     */
    function initProvinces() {
        $(#province).empty();
        $.ajax({
            type : "POST",
            url : basePath + "district/getProvinces.do",
            success : function(data) {
                $.each(data, function(i, it) {
                    $("<option value=‘" + it.id + "‘ />"
                        + it.name + "<br>").click(function() {
                            initCities(it.id);
                    }).appendTo($(#province));
                });
            }
       });
    }
    /**
     * 获取市列表
     */
    function initCities(provinceID) {
        $(#city).empty();
        $.ajax({
            type : "POST",
            url : basePath + "district/getCities.do?province=" + provinceID,
            success : function(data) {
                $.each(data, function(i, it) {
                    $("<option value=‘" + it.id + "‘ />"
                        + it.name + "<br>").click(function() {
                            initCounties(it.id);
                    }).appendTo($(#province));
                });
            }
        });
    }
    /**
     * 获取区县列表
     */
    function initCounties(cityID) {
        $(#county).empty();
        $.ajax({
            type : "POST",
            url : basePath + "district/getCounties.do?city=" + cityID,
            success : function(data) {
                $.each(data, function(i, it) {
                    $("<option value=‘" + it.id + "‘ />"
                        + it.name + "<br>")
                    .appendTo($(#province));
                });
            }
 });
}
//……
</script>
<body>
 选择地区:
 <select id=province><option>---省---</option></select>
 <select id=city><option>---市---</option></select>
 <select id=county><option>---区---</option></select>
</body>
spring MVC 代码:

@Controller
@RequestMapping(value = "/district")
public class districtController {
    @Resource
    private DistrictService districtService;
 /**
  * 获取省列表
  * @return
  * @throws Exception
  */
 @RequestMapping(value = "getProvinces")
 @ResponseBody
 public Object getProvinces() throws Exception {
    return districtService.getProvinces();
 }
 /**
  * 获取市列表
  * @param province
  * @return
  * @throws Exception
  */
 @RequestMapping(value = "getCities")
 @ResponseBody
 public Object getCities(@RequestParam(value = "province") String province) throws Exception {
    return districtService.getCities();
 }
    // 再往下级的获取方式和getCities方法都相同,所以此处略过
}

 

Spring MVC+JSP实现三级联动

标签:

原文地址:http://www.cnblogs.com/zhujiabin/p/4995591.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!