0
    var chooseCity={"provinces": [
            {
                "citys": [
                    {
                        "citysName": "石家庄市"
                    },
                    {
                        "citysName": "邯郸市"
                    },
                    {
                        "citysName": "唐山市"
                    },
                    {
                        "citysName": "保定市"
                    },
                    {
                        "citysName": "秦皇岛市"
                    },
                    {
                        "citysName": "邢台市"
                    },
                    {
                        "citysName": "张家口市"
                    },
                    {
                        "citysName": "承德市"
                    },
                    {
                        "citysName": "沧州市"
                    },
                    {
                        "citysName": "廊坊市"
                    },
                    {
                        "citysName": "衡水市"
                    },
                    {
                        "citysName": "辛集市"
                    },
                    {
                        "citysName": "晋州市"
                    },
                    {
                        "citysName": "新乐市"
                    },
                    {
                        "citysName": "遵化市"
                    },
                    {
                        "citysName": "迁安市"
                    },
                    {
                        "citysName": "霸州市"
                    },
                    {
                        "citysName": "三河市"
                    },
                    {
                        "citysName": "定州市"
                    },
                    {
                        "citysName": "涿州市"
                    },
                    {
                        "citysName": "安国市"
                    },
                    {
                        "citysName": "高碑店市"
                    },
                    {
                        "citysName": "泊头市"
                    },
                    {
                        "citysName": "任丘市"
                    },
                    {
                        "citysName": "黄骅市"
                    },
                    {
                        "citysName": "河间市"
                    },
                    {
                        "citysName": "冀州市"
                    },
                    {
                        "citysName": "深州市"
                    },
                    {
                        "citysName": "南宫市"
                    },
                    {
                        "citysName": "沙河市"
                    },
                    {
                        "citysName": "武安市"
                    }
                ],
                "provinceName": "河北省"
            },
            {
                "citys": [
                    {
                        "citysName": "太原市"
                    },
                    {
                        "citysName": "大同市"
                    },
                    {
                        "citysName": "朔州市"
                    },
                    {
                        "citysName": "阳泉市"
                    },
                    {
                        "citysName": "长治市"
                    },
                    {
                        "citysName": "晋城市"
                    },
                    {
                        "citysName": "忻州市"
                    },
                    {
                        "citysName": "吕梁市"
                    },
                    {
                        "citysName": "晋中市"
                    },
                    {
                        "citysName": "临汾市"
                    },
                    {
                        "citysName": "运城市"
                    },
                    {
                        "citysName": "古交市"
                    },
                    {
                        "citysName": "潞城市"
                    },
                    {
                        "citysName": "高平市"
                    },
                    {
                        "citysName": "原平市"
                    },
                    {
                        "citysName": "孝义市"
                    },
                    {
                        "citysName": "汾阳市"
                    },
                    {
                        "citysName": "介休市"
                    },
                    {
                        "citysName": "侯马市"
                    },
                    {
                        "citysName": "霍州市"
                    },
                    {
                        "citysName": "永济市"
                    },
                    {
                        "citysName": "河津市"
                    }
                ],
                "provinceName": "山西省"
            },
            {
                "citys": [
                    {
                        "citysName": "呼和浩特市"
                    },
                    {
                        "citysName": "包头市"
                    },
                    {
                        "citysName": "乌海市"
                    },
                    {
                        "citysName": "赤峰市"
                    },
                    {
                        "citysName": "呼伦贝尔市"
                    },
                    {
                        "citysName": "通辽市"
                    },
                    {
                        "citysName": "乌兰察布市"
                    },
                    {
                        "citysName": "鄂尔多斯市"
                    },
                    {
                        "citysName": "巴彦淖尔市"
                    },
                    {
                        "citysName": "满洲里市"
                    },
                    {
                        "citysName": "扎兰屯市"
                    },
                    {
                        "citysName": "牙克石市"
                    },
                    {
                        "citysName": "根河市"
                    },
                    {
                        "citysName": "额尔古纳市"
                    },
                    {
                        "citysName": "乌兰浩特市"
                    },
                    {
                        "citysName": "阿尔山市"
                    },
                    {
                        "citysName": "霍林郭勒市"
                    },
                    {
                        "citysName": "锡林浩特市"
                    },
                    {
                        "citysName": "二连浩特市"
                    },
                    {
                        "citysName": "丰镇市"
                    }
                ],
                "provinceName": "内蒙古自治区"
            },
            {
                "citys": [
                    {
                        "citysName": "沈阳市"
                    },
                    {
                        "citysName": "大连市"
                    },
                    {
                        "citysName": "朝阳市"
                    },
                    {
                        "citysName": "阜新市"
                    },
                    {
                        "citysName": "铁岭市"
                    },
                    {
                        "citysName": "抚顺市"
                    },
                    {
                        "citysName": "本溪市"
                    },
                    {
                        "citysName": "辽阳市"
                    },
                    {
                        "citysName": "鞍山市"
                    },
                    {
                        "citysName": "丹东市"
                    },
                    {
                        "citysName": "营口市"
                    },
                    {
                        "citysName": "盘锦市"
                    },
                    {
                        "citysName": "锦州市"
                    },
                    {
                        "citysName": "葫芦岛市"
                    },
                    {
                        "citysName": "新民市"
                    },
                    {
                        "citysName": "瓦房店市"
                    },
                    {
                        "citysName": "庄河市"
                    },
                    {
                        "citysName": "北票市"
                    },
                    {
                        "citysName": "凌源市"
                    },
                    {
                        "citysName": "调兵山市"
                    },
                    {
                        "citysName": "开原市"
                    },
                    {
                        "citysName": "灯塔市"
                    },
                    {
                        "citysName": "海城市"
                    },
                    {
                        "citysName": "凤城市"
                    },
                    {
                        "citysName": "东港市"
                    },
                    {
                        "citysName": "大石桥市"
                    },
                    {
                        "citysName": "盖州市"
                    },
                    {
                        "citysName": "凌海市"
                    },
                    {
                        "citysName": "北镇市"
                    },
                    {
                        "citysName": "兴城市"
                    }
                ],
                "provinceName": "辽宁省"
            },
            {
                "citys": [
                    {
                        "citysName": "长春市"
                    },
                    {
                        "citysName": "吉林市"
                    },
                    {
                        "citysName": "白城市"
                    },
                    {
                        "citysName": "松原市"
                    },
                    {
                        "citysName": "四平市"
                    },
                    {
                        "citysName": "辽源市"
                    },
                    {
                        "citysName": "通化市"
                    },
                    {
                        "citysName": "白山市"
                    },
                    {
                        "citysName": "德惠市"
                    },
                    {
                        "citysName": "榆树市"
                    },
                    {
                        "citysName": "磐石市"
                    },
                    {
                        "citysName": "蛟河市"
                    },
                    {
                        "citysName": "桦甸市"
                    },
                    {
                        "citysName": "舒兰市"
                    },
                    {
                        "citysName": "洮南市"
                    },
                    {
                        "citysName": "大安市"
                    },
                    {
                        "citysName": "双辽市"
                    },
                    {
                        "citysName": "公主岭市"
                    },
                    {
                        "citysName": "梅河口市"
                    },
                    {
                        "citysName": "集安市"
                    },
                    {
                        "citysName": "临江市"
                    },
                    {
                        "citysName": "延吉市"
                    },
                    {
                        "citysName": "图们市"
                    },
                    {
                        "citysName": "敦化市"
                    },
                    {
                        "citysName": "珲春市"
                    },
                    {
                        "citysName": "龙井市"
                    },
                    {
                        "citysName": "和龙市"
                    },
                    {
                        "citysName": "扶余市"
                    }
                ],
                "provinceName": "吉林省"
            },
            {
                "citys": [
                    {
                        "citysName": "哈尔滨市"
                    },
                    {
                        "citysName": "齐齐哈尔市"
                    },
                    {
                        "citysName": "黑河市"
                    },
                    {
                        "citysName": "大庆市"
                    },
                    {
                        "citysName": "伊春市"
                    },
                    {
                        "citysName": "鹤岗市"
                    },
                    {
                        "citysName": "佳木斯市"
                    },
                    {
                        "citysName": "双鸭山市"
                    },
                    {
                        "citysName": "七台河市"
                    },
                    {
                        "citysName": "鸡西市"
                    },
                    {
                        "citysName": "牡丹江市"
                    },
                    {
                        "citysName": "绥化市"
                    },
                    {
                        "citysName": "尚志市"
                    },
                    {
                        "citysName": "五常市"
                    },
                    {
                        "citysName": "讷河市"
                    },
                    {
                        "citysName": "北安市"
                    },
                    {
                        "citysName": "五大连池市"
                    },
                    {
                        "citysName": "铁力市"
                    },
                    {
                        "citysName": "同江市"
                    },
                    {
                        "citysName": "富锦市"
                    },
                    {
                        "citysName": "虎林市"
                    },
                    {
                        "citysName": "密山市"
                    },
                    {
                        "citysName": "绥芬河市"
                    },
                    {
                        "citysName": "海林市"
                    },
                    {
                        "citysName": "宁安市"
                    },
                    {
                        "citysName": "安达市"
                    },
                    {
                        "citysName": "肇东市"
                    },
                    {
                        "citysName": "海伦市"
                    },
                    {
                        "citysName": "穆棱市"
                    },
                    {
                        "citysName": "东宁市"
                    },
                    {
                        "citysName": "抚远市"
                    }
                ]
    
    
    };//太长选择了一段
    
    for( i in chooseCity){
        
        for( j in chooseCity[i]){
            $('.sort_box').append('<div class="sort_list"><div class="num_name">'+chooseCity[i][j].provinceName+'</div></div>')
            
            for(c in chooseCity[i][j].citys){
              $('.num_name').append('<p value="">'+chooseCity[i][j].citys[c].citysName+'</p>'
              )
                
        }
                
            }    
            
    }

如何把二级市循环到每个省下面呢?

3个回答

0

已采纳
for(i in chooseCity) {
    for (j in chooseCity[i]) {
      var cArr = []
      for (c in chooseCity[i][j].citys) {
        cArr.push('<p value="">' + chooseCity[i][j].citys[c].citysName + '</p>')
      }
      $('.sort_box').append('<div class="sort_list"><div class="num_name">' + chooseCity[i][j].provinceName + cArr.join('') + '</div></div>')
    }
  }
1

你的 $('.num_name')是获取所有的.num_name
改进一下

var str = "";
for( i in chooseCity){
    for( j in chooseCity[i]){
        str += '<div class="sort_list"><div class="num_name">'+chooseCity[i][j].provinceName
        for(c in chooseCity[i][j].citys){
          str +='<p value="">'+chooseCity[i][j].citys[c].citysName+'</p>'
            }
        str += '</div></div>';
       }
}
$('.sort_box').append(str);
1

循环两次就行,我给你提个思路,
第一层循环chooseCity的时候:chooseCity[0],chooseCity[1]....等等,他下面是不是会有[[citys:[0],[1]...],provinceName:省份];
这个时候里面在来一层循环,相当于循环的是chooseCity[0]....等等;
那么你把每个chooseCity[0]..下面的provinceName的值放入新的数组中作为键,把chooseCity[0]..下面的citys放入这个键中,是不是就都归入省份下了。希望可以帮到你

撰写答案