﻿/**
 * jQuery :  省市联动插件
 * @author   
 *			
 * @example  $("#test").ProvinceCity(["province","city"]);
 * @params   暂无
 */
$.fn.ProvinceCity = function(id,name){
	var _self = this;
	//定义2个默认值
	_self.data("province",["请选择", "请选择"]);
	_self.data("city1",["请选择", "请选择"]);
	//插入2个空的下拉框
	_self.append("<span>省</span><select></select>");
	_self.append("<span>市</span><select></select>");
	//分别获取2个下拉框
	var $sel1 = _self.find("select").eq(0);
	var $sel2 = _self.find("select").eq(1);
	if(id!=undefined){
		$sel1.attr("id",id[0]);
		$sel2.attr("id",id[1]);
	}
	if(name!=undefined){
		$sel1.attr("name",name[0]);
		$sel2.attr("name",name[1]);
	}
	//默认省级下拉(默认值为空)
	if(_self.data("province")){
		$sel1.append("<option value=''>"+_self.data("province")[0]+"</option>");
	}
	
	$.each( GP , function(index,data){
		$sel1.append("<option value='"+data+"'>"+data+"</option>");
	});
	
	//默认的城市下拉(默认值为空)
	if(_self.data("city1")){
		$sel2.append("<option value=''>"+_self.data("city1")[0]+"</option>");
	}
	
	//省级联动 控制
	var index1 = "" ;
	$sel1.change(function(){
		//清空其它2个下拉框
		$sel2[0].options.length=0;
		index1 = this.selectedIndex;
		if(index1==0){	//当选择的为 “请选择” 时
			if(_self.data("city1")){
				$sel2.append("<option value='"+_self.data("city1")[1]+"'>"+_self.data("city1")[0]+"</option>");
			}
			
		}else{
			$.each(GT[index1-1] , function(index,data){
				$sel2.append("<option value='"+data+"'>"+data+"</option>");
			});			
		}
	});
	
	return _self;
};