三級聯動查詢

$(function() {

    $("#province").change(function() {
        // 獲得選中的省id
        var areaId = $(this).val();
        loadOption(areaId, "mycity");
    });

    $("#mycity").change(function() {
        // 獲得選中的市id
        var areaId = $(this).val();
        loadOption(areaId, "district");
    });

    $("#jvForm").submit(function() {

    })

});

function loadOption(areaId, selectId) {

    $.ajax({
        url : getRootPath() + "/equipment/getChildArea.action",
        type : "post",
        dataType : "text",
        async : false,
        data : {
            areaId : areaId
        },
        success : function(responseText) {
            // alert(responseText);
            var jsonObj = $.parseJSON(responseText);
            var aList = jsonObj.armmy;

            if (selectId == "mycity") {
                $("#mycity").empty();
                $("#district").empty();
                $("#mycity").append("<option value='-1'>連</option>");
                $("#district").append("<option value='-1'>排</option>");
            } else {
                $("#district").empty();
                $("#" + selectId).append("<option value=''>排</option>");
            }
            if (aList != null && aList.length > 0) {
                var option = "";
                // 數組不能用each不來遍歷
                for (var i = 0; i < aList.length; i++) {
                    // alert(aList[i].id);
                    option = option + "<option value='" + aList[i].id + "'>"
                            + aList[i].name + "</option>";
                }
                $("#" + selectId).append(option);

            }

        }
    })
}

 

@RequestMapping("getChildArea")
    public void getChildArea(int areaId, HttpServletResponse response) {
        List<EqArmunit> armunit = depService.selectChildrenById(areaId);
        JSONObject jo = new JSONObject();
        jo.accumulate("armmy", armunit);
        String result = jo.toString();
        ECPSUtils.printAjax(response, result);

    }

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章