Home > Web Front-end > JS Tutorial > body text

jquery regional linkage effect code based on json_jquery

WBOY
Release: 2016-05-16 18:05:05
Original
1123 people have browsed it

The original intention of writing this thing came from Sina Weibo. There is a region option in the Sina Weibo account settings. It is written in js. I wanted to intercept its code, but I was disappointed that its js code was compressed. , but the json type format I saw is quite well designed. Generally, the json data format without any processing in our background is similar to the following
[{"Code":3231,"Name":"Changchun City" },
{"Code":3232,"Name":"Jilin City}]
If there are 10,000 regions, there will be tens of thousands more characters. I think I should give up such a luxurious approach.
Out of this idea, I wrote json output in this format using .NET, and wrote a drop-down based on jquery linkage.
The main code is as follows:
json code:
<.>

Copy code The code is as follows:

var area = '{"city3145":"市辖区",
"code3145":"3179",
"city3146":"市辖区",
"code3146":"3180",
"city3147":"石家庄市,唐山市,秦皇岛市,邯郸市,邢台市,保定市,张家口市,承德市,沧州市,廊坊市,衡水市",
"code3147":"3181,3182,3183,3184,3185,3186,3187,3188,3189,3190,3191",
"city3148":"太原市,大同市,阳泉市,长治市,晋城市,朔州市,晋中市,运城市,忻州市,临汾市,吕梁市",
"code3148":"3192,3193,3194,3195,3196,3197,3198,3199,3200,3201,3202",
"city3149":"呼和浩特市,包头市,乌海市,赤峰市,通辽市,鄂尔多斯市,呼伦贝尔市,巴彦淖尔市,乌兰察布市,兴安盟,锡林郭勒盟,阿拉善盟",
"code3149":"3203,3204,3205,3206,3207,3208,3209,3210,3211,3212,3213,3214",
"city3150":"沈阳市,大连市,鞍山市,抚顺市,本溪市,丹东市,锦州市,营口市,阜新市,辽阳市,盘锦市,铁岭市,朝阳市,葫芦岛市",
"code3150":"3215,3216,3217,3218,3219,3220,3221,3222,3223,3224,3225,3226,3227,3228",
"city3151":"长春市,吉林市,四平市,辽源市,通化市,白山市,松原市,白城市,延边朝鲜族自治州",
"code3151":"3229,3230,3231,3232,3233,3234,3235,3236,3237",
"city3152":"哈尔滨市,齐齐哈尔市,鸡西市,鹤岗市,双鸭山市,大庆市,伊春市,佳木斯市,七台河市,牡丹江市,黑河市,绥化市,大兴安岭地区",
"code3152":"3238,3239,3240,3241,3242,3243,3244,3245,3246,3247,3248,3249,3250",
"city3153":"市辖区", "code3153":"3251",
"city3154":"南京市,无锡市,徐州市,常州市,苏州市,南通市,连云港市,淮安市,盐城市,扬州市,镇江市,泰州市,宿迁市",
"code3154":"3252,3253,3254,3255,3256,3257,3258,3259,3260,3261,3262,3263,3264",
"city3155":"杭州市,宁波市,温州市,嘉兴市,湖州市,绍兴市,金华市,衢州市,舟山市,台州市,丽水市",
"code3155":"3265,3266,3267,3268,3269,3270,3271,3272,3273,3274,3275",
"city3156":"合肥市,芜湖市,蚌埠市,淮南市,马鞍山市,淮北市,铜陵市,安庆市,黄山市,滁州市,阜阳市,宿州市,巢湖市,六安市,亳州市,池州市,宣城市",
"code3156":"3276,3277,3278,3279,3280,3281,3282,3283,3284,3285,3286,3287,3288,3289,3290,3291,3292",
"city3157":"福州市,厦门市,莆田市,三明市,泉州市,漳州市,南平市,龙岩市,宁德市",
"code3157":"3293,3294,3295,3296,3297,3298,3299,3300,3301",
"city3158":"南昌市,景德镇市,萍乡市,九江市,新余市,鹰潭市,赣州市,吉安市,宜春市,抚州市,上饶市",
"code3158":"3302,3303,3304,3305,3306,3307,3308,3309,3310,3311,3312",
"city3159":"济南市,青岛市,淄博市,枣庄市,东营市,烟台市,潍坊市,济宁市,泰安市,威海市,日照市,莱芜市,临沂市,德州市,聊城市,滨州市,荷泽市",
"code3159":"3313,3314,3315,3316,3317,3318,3319,3320,3321,3322,3323,3324,3325,3326,3327,3328,3329",
"city3160":"郑州市,开封市,洛阳市,平顶山市,安阳市,鹤壁市,新乡市,焦作市,濮阳市,许昌市,漯河市,三门峡市,南阳市,商丘市,信阳市,周口市,驻马店市",
"code3160":"3330,3331,3332,3333,3334,3335,3336,3337,3338,3339,3340,3341,3342,3343,3344,3345,3346",
"city3161":"武汉市,黄石市,十堰市,宜昌市,襄樊市,鄂州市,荆门市,孝感市,荆州市,黄冈市,咸宁市,随州市,恩施土家族苗族自治州,省直辖行政单位",
"code3161":"3347,3348,3349,3350,3351,3352,3353,3354,3355,3356,3357,3358,3359,3360",
"city3162":"长沙市,株洲市,湘潭市,衡阳市,邵阳市,岳阳市,常德市,张家界市,益阳市,郴州市,永州市,怀化市,娄底市,湘西土家族苗族自治州",
"code3162":"3361,3362,3363,3364,3365,3366,3367,3368,3369,3370,3371,3372,3373,3374",
"city3163":"广州市,韶关市,深圳市,珠海市,汕头市,佛山市,江门市,湛江市,茂名市,肇庆市,惠州市,梅州市,汕尾市,河源市,阳江市,清远市,东莞市,中山市,潮州市,揭阳市,云浮市",
"code3163":"3375,3376,3377,3378,3379,3380,3381,3382,3383,3384,3385,3386,3387,3388,3389,3390,3391,3392,3393,3394,3395",
"city3164":"南宁市,柳州市,桂林市,梧州市,北海市,防城港市,钦州市,贵港市,玉林市,百色市,贺州市,河池市,来宾市,崇左市",
"code3164":"3396,3397,3398,3399,3400,3401,3402,3403,3404,3405,3406,3407,3408,3409",
"city3165":"海口市,三亚市,省直辖县级行政单位",
"code3165":"3410,3411,3412",
"city3166":"市辖区",
"code3166":"3413",
"city3167":"成都市,自贡市,攀枝花市,泸州市,德阳市,绵阳市,广元市,遂宁市,内江市,乐山市,南充市,眉山市,宜宾市,广安市,达州市,雅安市,巴中市,资阳市,阿坝藏族羌族自治州,甘孜藏族自治州,凉山彝族自治州",
"code3167":"3415,3416,3417,3418,3419,3420,3421,3422,3423,3424,3425,3426,3427,3428,3429,3430,3431,3432,3433,3434,3435",
"city3168":"Guiyang City, Liupanshui City, Zunyi City, Anshun City, Tongren area, Qianxinan Buyi and Miao Autonomous Prefecture, Bijie area, Qiandongnan Miao and Dong Autonomous Prefecture, Qiannan Buyi and Miao Autonomous Prefecture",
"code3168" :"3436,3437,3438,3439,3440,3441,3442,3443,3444",
"city3169":"Kunming City, Qujing City, Yuxi City, Baoshan City, Zhaotong City, Lijiang City, Simao City, Lincang City, Chuxiong Yi Autonomous Prefecture, Honghe Hani and Yi Autonomous Prefecture, Wenshan Zhuang and Miao Autonomous Prefecture, Xishuangbanna Dai Autonomous Prefecture, Dali Bai Autonomous Prefecture, Dehong Dai and Jingpo Autonomous Prefecture, Nujiang Lisu Autonomous Prefecture, Diqing Tibetan Autonomous Prefecture",
"code3169 ":"3445,3446,3447,3448,3449,3450,3451,3452,3453,3454,3455,3456,3457,3458,3459,3460",
"city3170":"Lhasa City, Qamdo Region, Shannan Region, Shigatse Region, Nagqu Region, Ali Region, Nyingchi Region",
"code3170":"3461,3462,3463,3464,3465,3466,3467",
"city3171":"Xi'an City, Tongchuan City, Baoji City, Xianyang City, Weinan City, Yan'an City, Hanzhong City, Yulin City, Ankang City, Shangluo City",
"code3171":"3468,3469,3470,3471,3472,3473,3474, 3475,3476,3477",
"city3172":"Lanzhou City, Jiayuguan City, Jinchang City, Baiyin City, Tianshui City, Wuwei City, Zhangye City, Pingliang City, Jiuquan City, Qingyang City, Dingxi City, Longnan City City, Linxia Hui Autonomous Prefecture, Gannan Tibetan Autonomous Prefecture",
"code3172":"3478,3479,3480,3481,3482,3483,3484,3485,3486,3487,3488,3489,3490,3491",
"city3173":"Xining City, Haidong Region, Haibei Tibetan Autonomous Prefecture, Huangnan Tibetan Autonomous Prefecture, Hainan Tibetan Autonomous Prefecture, Goluo Tibetan Autonomous Prefecture, Yushu Tibetan Autonomous Prefecture, Hainan Mongolian and Tibetan Autonomous Prefecture",
"code3173": "3492,3493,3494,3495,3496,3497,3498,3499",
"city3174":"Yinchuan City, Shizuishan City, Wuzhong City, Guyuan City, Zhongwei City",
"code3174":" 3500,3501,3502,3503,3504",
"city3175":"Urumqi City, Karamay City, Turpan Region, Hami Region, Changji Hui Autonomous Prefecture, Bortala Mongolian Autonomous Prefecture, Bayingol Mongolian Autonomous Prefecture, Aksu Region, Kizilsu Kirgiz Autonomous Prefecture, Kashgar Prefecture, Hotan Prefecture, Ili Kazakh Autonomous Prefecture, Tacheng Prefecture, Altay Prefecture, provincial administrative units",
"code3175":"3505,3506,3507,3508,3509, 3510,3511,3512,3513,3514,3515,3516,3517,3518,3519",
"city3176":"",
"code3176":"",
"city3177":"" ,
"code3177":"",
"city3178":"",
"code3178":"",
provinces:"Beijing, Tianjin, Hebei Province, Shanxi Province, Inner Mongolia Autonomous Region, Liaoning Province, Jilin Province, Heilongjiang Province, Shanghai City, Jiangsu Province, Zhejiang Province, Anhui Province, Fujian Province, Jiangxi Province, Shandong Province, Henan Province, Hubei Province, Hunan Province, Guangdong Province, Guangxi Zhuang Autonomous Region, Hainan Province , Chongqing City, Sichuan Province, Guizhou Province, Yunnan Province, Tibet Autonomous Region, Shaanxi Province, Gansu Province, Qinghai Province, Ningxia Hui Autonomous Region, Xinjiang Uygur Autonomous Region, Taiwan Province, Hong Kong Special Administrative Region, Macau Special Administrative Region",
provcodes: "3145,3146,3147,3148,3149,3150,3151,3152,3153,3154,3155,3156,3157,3158,3159,3160,3161,3162,3163,3164,3165,3166,3167,3168,3 169 ,3170,3171,3172,3173,3174,3175,3176,3177,3178"}';

jquery code, key code
Copy code The code is as follows:

$(function(){
//Get the json object
var jsonobj = eval('(' area ')');
//Province number
var provcodes = jsonobj.provcodes;
//Province number array
var provcodesArray = provcodes.split(',');
// Number of province codes
var provcodesLength = provcodesArray.length;
//Province name
var provinces = jsonobj.provinces;
var provincesArray = provinces.split(',');
var provhtml = '';
//Bind province
for(var i=0;iprovhtml = '';
}
$("#province").append(provhtml);
//Select a provincial city to load the lower level
$("#province").change(function(){
var cityhtml = '';
}
$("#city").empty().append(cityhtml) ;
});
});

Full code:
Copy codeThe code is as follows:




New Document







<script> <br></script>
<script> <br>//Just paste the above code<br></script>


Region:





The next step is the .NET code data json,
Copy the code The code is as follows:

///
/// Output json data
///

///
/// Date:2011-07-01
/// Author:Peaceful Age
///

///
public string WriteJson()
{
//Get all the data in the database
Ilist areaList = GetAreaTypeListAll();
//Get the omissions Level collection, AT_ParentId=1
//var provNameParentId = from area in areaList where area.AT_ParentId == 1 select area;
//All data
var areaLinq = from area in areaList select area;
//Filter, filter out provincial collections, filter and adjust AT_ParentId == 0
var areaWhereParentId = areaLinq.Where(m => m.AT_ParentId == 0).ToList();
// Provincial name format: "Hunan Province, Zhejiang Province, Beijing City,..."
string provName = string.Join(",", areaWhereParentId.Select(m => m.AT_Name).ToArray()) ;
//Provincial number format: "1,2,3,..."
string provCode = string.Join(",", areaWhereParentId.Select(m => m.AT_Id).ToArray ());
//Provincial number
int num = areaWhereParentId.Count();
var jsonstr = new StringBuilder();
jsonstr.Append("{");
var areaCityList = new List();
for(var i=0;i{
var areaType = new AreaType();
areaType = areaWhereParentId[i ] ;
int id = areaType.AT_Id;
areaCityList = areaLinq.Where(m => m.AT_ParentId == id).ToList();
var cityName = string.Join("," , areaCityList.Select(m => m.AT_Name).ToArray());
var cityCode = string.Join(",", areaCityList.Select(m => m.AT_Id).ToArray()) ;
jsonstr.Append(""");
jsonstr.Append("city");
jsonstr.Append(areaType.AT_Id);
jsonstr.Append(""");
jsonstr.Append(":");
jsonstr.Append(""");
jsonstr.Append(cityName);
jsonstr.Append(""");
jsonstr. Append(",");
jsonstr.Append(""");
jsonstr.Append("code");
jsonstr.Append(areaType.AT_Id);
jsonstr.Append( """);
jsonstr.Append(":");
jsonstr.Append(""");
jsonstr.Append(cityCode);
jsonstr.Append(""") ;
jsonstr.Append(",");
}
jsonstr.Append("provinces:");
jsonstr.Append(""");
jsonstr.Append(provName );
jsonstr.Append(""");
jsonstr.Append(",");
jsonstr.Append("provcodes:");
jsonstr.Append(""") ;
jsonstr.Append(provCode);
jsonstr.Append(""");
jsonstr.Append("}");
return jsonstr.ToString();
}

The region table is roughly designed as
AT_Id: unique identifier, corresponding code in json
AT_Name: Chinese name of the region
AT_ParentId: parent id
AT_Level: level, 1 For provincial level, 2 for municipal level
The above code is for reference only, the code quality and efficiency cannot be guaranteed, use it at your own risk..
I hope to get everyone's valuable opinions and better solution-level methods

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template