基于Java的ajax城市三级联动

ajax城市三级联动,网上搜的一个示例,找不到原著作者,在此抱歉。

首先是页面和JS代码

<!--  省城市三级联动 -->
<div class="form-group">
                                                            <label class="control-label col-md-3 ">城市
<span class="required"> * </span>
</label>

                                                            <div class="col-md-1  ">
                                                                <select name="pId" id="pId" class="form-control">
                                                                <option value="AL">请选择</option>
                                                                    <c:forEach items ="${provinceList}" var = "city">
                                                                    <option value="${city.id}">${city.p}</option>
                                                                    </c:forEach>                                                                
                                                                    </select>
                                                            </div>
<div class="col-md-1 ">
                                                                <select name="cId" id="cId" class="form-control">
                                                                </select>
                                                            </div>
<div class="col-md-1 ">
                                                                <select name="yId" id="yId" class="form-control">
                                                                </select>
                                                            </div>
                                                        </div>


//城市二级联动 市
$("#pId").change(function() {
//当改变省份时需先清空城市列表和区域列表  
$("#cId option:not(:first)").remove();
$("#yId option:not(:first)").remove();
var url = "${pageContext.request.contextPath}/selectMunicipality?id="+ new Date().getTime(); //模糊查询
var pId = $("#pId option:selected").val();
var sendData = {"method" : "TestC","pId" : pId};

//使用post方式提交数据        
$.post(url,sendData,function(backData) {var json = eval("("+ backData+ ")");
for (var i = 0; i < json.length; i++) {
if (json[i].c != null) {
$("#cId").append($("<option value="+json[i].id+">"+ json[i].c+ "</option>"));
}
}
});
});

后台代码

   //城市信息分类二级联动
@RequestMapping(value="/selectMunicipality", method = RequestMethod.POST)
    @ResponseBody
    public String do( HttpServletRequest request,Model model){
String str1=request.getParameter("pId");

    int  pId=Integer.parseInt(str1);
   
    List<City> list=cityService.selectMunicipality(pId);
    String str = JSONUtils.toJson(list);//将list转化为JSON;
     
    return str;
    }


需要注意的点是,list集合需转成json返回给前端解析。个人是小白希望各位大神告知具体原因


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值