我爱编程

jquery通过后端请求的区域四级联动

2018-08-09  本文已影响0人  前端一枝花

1.大致思路

2.代码解释

<1> 变量province、city、district 、district 、分别对应 省 市 区 街道 四个select

<2> 对象getArea有五个方法


var province = $("#province");
var city = $("#city");
var district = $("#district"); 
var district = $("#town"); 
var getArea = { 
  emptySelect: function(index){ 
    for (var i=index; i<4;i++){ 
      $($(".areaSelect")[i]).empty();
    }        
  }, 
  init:async function (provinceCode, cityCode, districtCode, townCode) { 
         await $.post('/system/area/province',function (res) {
                for (var local in res) {
                    province.append("<option value=" + res[local].provinceCode + ">" + res[local].provinceName + "</option>");
                }
                if (provinceCode != "") {
                    console.log("provinceCode"+provinceCode);
                    console.log($("#province option[value='330000']"))
                    $("#province option[value='"+provinceCode+"']").attr("selected", true);
                }
          });
            await $.post('/system/area/city?provinceCode='+provinceCode,function (res) {
                console.log("province select"+province.val());
                for (var local in res) {
                    city.append("<option value=" + res[local].cityCode + ">" + res[local].cityName + "</option>");
                }
                if (cityCode != "") {
                    console.log("cityCode"+cityCode);
                    $("#city option[value='"+cityCode+"']").attr("selected", true);
                }
            });

            await $.post('/system/area/district?cityCode='+cityCode,function (res) {
                console.log("city select"+city.val());
                for (var local in res) {
                    district.append("<option value=" + res[local].districtCode + ">" + res[local].districtName + "</option>");
                }
                if (districtCode != "") {
                    console.log("districtCode"+districtCode);
                    $("#district option[value='"+districtCode+"']").attr("selected", true);
                }
            });

            await $.post('/system/area/town?districtCode='+districtCode,function (res) {
                console.log("district select"+district.val());
                for (var local in res) {
                    town.append("<option value=" + res[local].townCode + ">" + res[local].townName + "</option>");
                }
                if (townCode != "") {
                    console.log("townCode"+townCode);
                    $("#town option[value='"+townCode+"']").attr("selected", true);
                }
            });
        },
        provinceChange:async function () {
            await $.post('/system/area/city?provinceCode='+province.val(),function (res) {
                console.log("province select"+province.val());
                for (var local in res) {
                    city.append("<option value=" + res[local].cityCode + ">" + res[local].cityName + "</option>");
                }
            });

            await $.post('/system/area/district?cityCode='+city.val(),function (res) {
                console.log("city select"+city.val());
                for (var local in res) {
                    district.append("<option value=" + res[local].districtCode + ">" + res[local].districtName + "</option>");
                }
            });

            await $.post('/system/area/town?districtCode='+district.val(),function (res) {
                console.log("district select"+district.val());
                for (var local in res) {
                    town.append("<option value=" + res[local].townCode + ">" + res[local].townName + "</option>");
                }
            });
        },
        cityChange:async function(){
            await $.post('/system/area/district?cityCode='+city.val(),function (res) {
                console.log("city select"+city.val());
                for (var local in res) {
                    district.append("<option value=" + res[local].districtCode + ">" + res[local].districtName + "</option>");
                }
            });

            await $.post('/system/area/town?districtCode='+district.val(),function (res) {
                console.log("district select"+district.val());
                for (var local in res) {
                    town.append("<option value=" + res[local].townCode + ">" + res[local].townName + "</option>");
                }
            });
        },
        districtChange:async function(){
            await $.post('/system/area/town?districtCode='+district.val(),function (res) {
                console.log("district select"+district.val());
                for (var local in res) {
                    town.append("<option value=" + res[local].townCode + ">" + res[local].townName + "</option>");
                }
            });
        }

    }
     
    getArea.init(330000,330100,330102,330102004);


    province.change( function () {
        if (province.val() != "") {
            getArea.emptySelect(1);
            getArea.provinceChange();
        }
    })

    city.change(function () {
        if (city.val() != "") {
            getArea.emptySelect(2);
            getArea.cityChange();
        }
    })

    district.change(function () {
        if (district.val() != "") {
            getArea.emptySelect(3);
            getArea.districtChange();
        }
    })
上一篇 下一篇

猜你喜欢

热点阅读