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

1.大致思路

  • 一开始页面加载根据省市区街道四个区域码设置select默认选中状态

  • 当省级select改变时,后面三级select重新请求

  • 当市级select改变时, 后面两级select重新请求

  • 以此类推

2.代码解释

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

<2> 对象getArea有五个方法

  • emptySelect(index): 清空 第(index)个select之后 几个select的内容(避免重复追加,如:省级select改变,市 区 街道三个select均要清空)

  • init(provinceCode, cityCode, districtCode, townCode): 设置四个select默认选中的状态(先从后端获取数据再选中)

  • provinceChange():当省级select改变时,市 区 街道重新发起请求

  • cityChange():当市级select改变时,区 街道重新发起请求

  • districtChange():当市级select改变时, 街道重新发起请求


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();
        }
    })
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 红尘事,万千言,纷扰在心田。 一壶浊酒慰流年,月明秋夜寒。 影阑珊,花无缺,玉盏频邀裂。 明朝梦醒返人间,笑言当尽欢。
    花屋主人萧寒阅读 535评论 1 10
  • 受老大哥之邀作同学聚会之感怀献丑啦! 同学聚会,其意切切,其乐融融。忆当初,学园清幽书声朗朗,...
    钝角阅读 658评论 0 37
  • 一篇文章,必须有一个合适的切入点,引出所要表达的观点,这样文章才有价值。如果东说一句西说一句,没有合适的切入点,那...
    奋斗在而立之年阅读 695评论 0 51
  • 时有一歌词动人心弦,借暖阳温暖人间。引之开篇!时于阳台读书,清风翻书,是有此言。 不用借 这个冬季很慷慨 将暖阳铺...
    一冉山阅读 268评论 0 0