jquery implementation of the three-level linkage menu of provinces and counties

Source: Internet
Author: User

Using jquery implementation of the national provincial and municipal three-level linkage menu, no use of Ajax, data is not dynamically read from the database, is simple jquery, convenient and practical, the realization of the effect of the following figure:

1, the definition of data, we can be provinces, cities and counties defined as a JS file, three of the array storage, the following is an example, so only some of the provinces, cities, counties, national and overseas all the data see my resources file: Provincial and county level three Linkage menu complete project provincesdata.js file, resources download address: http://download.csdn.net/detail/harderxin/8311607

We define a JS file that is used to store provincial and county data: Provincesdata.js

Define provincial data for one-dimensional array var GP =[' Anhui ', ' Macau ', ' Beijing ']//define city-level data, for two-dimensional array var GT = [[' Hefei ', ' anqing ', ' Bengbu ', ' Bozhou ', ' Chaohu ', ' Chizhou ', ' Chuzhou ', ' Fuyang ', ' Huaibei ', ' Huainan ', ' Huangshan ', ' Liu'an ', ' ma ' Anshan ', ' Suzhou ', ' tongling ', ' Wuhu ', ' Xuancheng '], [' Macau '], [' changping ', ' Chaoyang ', ' Chongwen ', ' daxing ', ' Dongcheng ', ' Fangshan ', ' Fengtai ', ' Haidian ', ' Huairou ', ' Mentougou ', ' Miyun ', ' Pinggu ', ' Shijingshan ' , ' Shunyi ', ' Tongzhou ', ' Xicheng ', ' Xuanwu ', ' Yanqing ']]//define county-level data, for three-dimensional array var GC = [[[' Changfeng ', ' Feidong ', ' Feixi ', ' Hefei '], [' Anqing ', ' Zongyang ', ' Huaining ', ' Qianshan ', ' Susong ', ' Taihu ', ' Tongcheng '
		, ' Wangjiang ', ' Yuexi '], [' Bangbu ', ' Guzhen ', ' Huaiyuan ', ' Wuhe '], [' Bozhou ', ' Lixin ', ' Mengcheng ', ' Woyang '], [' Chaohu ', ' Hanshan ', ' counties ', ' Lujiang ', ' inaction '], [' Chizhou ', ' Dongzhi ', ' Qingyang ', ' ' Stone '], [' Chuzhou ', ' Dingyuan ', ' Fengyang ', ' Laian ', ' Ming Guang ', ' Quanjiao ', ' Tianchang '], [' Funan ', ' Fuyang ', ' Jieshou ', ' Linquan ', ' Taihe ', ' stand on '], [' Huaibei ', ' Suixi '], [' Fengtai ', ' Huainan '], [' Huangshan ', '] ' Qimen ' , ' County ', ' Xiuning ', ' Yi County '], [' Huoqiu ', ' Huo Shan ', ' Jinzhai ', ' Liu'an ', ' Shouxian ', ' Shucheng ', [' Dangtu ', ' Maanshan '], [' Dangshan ', ' Lingbi ', ' Si County ', ' Suzhou ', ' Xiaoxian '], [' Tongling ', ' Tongling County '], [ ' Fanchang ', ' Nanling ', ' Wuhu ', ' Wuhu County ', [' Guangde ', ' Jixi ', ' Jing County ', ' Jingde ', ' Langxi ', ' Ningguo ', ' Xuancheng '], [[' Macau ']], [['] [' changping '], [' Chaoyang '], [' Chongwen '], [' Big Hing '], [' Dongcheng '], [' Fangshan '], [' Fengtai '], [' Haidian '], [' Huairou '], [' Mentougou '], [' Miyun '], [' Pinggu '], [' Shijingshan '], [' Shunyi '], [' Tongzhou '], [' West
 City '], [' Xuanwu '], [' Yanqing ']]]

2, write jquery city linkage plug-ins: jquery. Provincecity.js file

$.fn.
	provincecity = function () {var _self = this;
	Define 3 default values _self.data ("Province", ["Please select", "select"]);
	_self.data ("City1", ["Please select", "select"]);
	_self.data ("City2", ["Please select", "select"]);
	Insert 3 empty drop-down box _self.append ("<select></select>");
	_self.append ("<select></select>");
	_self.append ("<select></select>");
	Get 3 Drop-down boxes var $sel 1 = _self.find ("select"). EQ (0);
	var $sel 2 = _self.find ("select"). EQ (1);
	var $sel 3 = _self.find ("select"). EQ (2); Default provincial Drop-down if (_self.data ("province")) {$sel 1.append ("<option value=" +_self.data ("province") [1]+ ' > ' +_
	Self.data ("province") [0]+ "</option>");
	$.each (GP, function (index,data) {$sel 1.append ("<option value= '" +data+ "' >" +data+ "</option>");
	}); The default Level 1 city Drop-down if (_self.data ("City1")) {$sel 2.append ("<option value=" +_self.data ("City1") [1]+ ' > ' +_self.data (
	"City1") [0]+ "</option>"); ///Default Level 2 City dropdown if (_self.data ("City2")) {$sel 3.append ("<option value= '" +_self.data ("City2") [1]+ "' >" +_self.data ("City2") [0]+ "</option>");
	///Provincial linkage control var index1 = "";
		$sel 1.change (function () {//Empty the other 2 drop-down boxes $sel 2[0].options.length=0;
		$sel 3[0].options.length=0;
		Index1 = This.selectedindex; if (index1==0) {//When selected as "Please select" if (_self.data ("City1")) {$sel 2.append ("<option value= '" +_self.data ("City1") [1]+]
			> "+_self.data (" City1 ") [0]+" </option> "); } if (_self.data ("City2")) {$sel 3.append ("<option value=" +_self.data ("City2") [1]+ "' >" +_self.data ("City2") [0
			]+ "</option>"); }}else{$.each (Gt[index1-1], function (index,data) {$sel 2.append ("<option value=" "+data+" ' > "+data+" </o
			Ption> ");
			}); $.each (Gc[index1-1][0], function (index,data) {$sel 3.append ("<option value= '" +data+ "' >" +data+ "</option
			> ");
	}). change ();
	1-level city linkage control var index2 = "";
		$sel 2.change (function () {$sel 3[0].options.length=0;
		Index2 = This.selectedindex; $.each (Gc[index1-1][index2], function (index,data) {$sel 3. Append ("<option value= '" +data+ "' >" +data+ "</option>");
	})
	});
return _self; };

3, write our test page, import our city and county data js:provincesdata.js and jquery linkage plug-in js:jquery.ProvinceCity.js, There is also a very important jquery plugin: jquery-1.6.min.js, define a div file, introduce our provincecity method can:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >  


Look at the HTML file, you can see the effect, when we need to use the project, we can use the following several files as Plug-ins to be used directly in the project to add, if you need the appropriate data, in the provincedata.js to add and modify the corresponding, very convenient

<!--introduce jquery plugin-->
  <script src= "jquery-1.6.min.js" type= "Text/javascript" ></script>
  <!--introduced us to the provinces and counties of the <script jquery plugin-->
  src= "jquery." Provincecity.js "type=" Text/javascript ></script>
  <!--the introduction of the JS Provinces and cities of our country to write the basic data-->
  <script src= "Provincesdata.js" type= "Text/javascript" ></script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.