javacript实现根据选择不同国家选择显示不同的省/州

/**
 * @author wangxiang
 * @email:[email protected]
 * @copyright privite
 */
 
var countryProvcity = function(data){
    this.data = data;
}
countryProvcity.prototype = {
    init: function (){
            var _t = this;
              var _d = this.data;
            _d.cid = _d.cid?_d.cid:"cid"; //country id
            _d.pid = _d.pid ? _d.pid : "pid";//city id
            _d.pnum = _d.pnum ? _d.pnum : "pnum";//country number
            //_d.cnum = _d.cnum ? _d.cnum : "";//city number
            _d.box = _d.box ? _d.box : "box"; //container id
             var provinces = {
             "United states":1000840000,"Canada":1001124000,"Afghanistan":3040004000,"Albania":2200008000,"Algeria":5010012000,"American Samoa":3070016000,"Andorra":2100020000,"Angola":5020024000,"Anguilla":6010660000,"Antigua and Barbuda":6010028000,"Argentina":6030032000,"Armenia":3040051000,"Aruba":6010533000,"Australia":3010036000,"Austria":2100040000,"Azerbaijan":3040031000,"Bahamas":6010044000,"Bahrain":4000048000,"Bangladesh":3050050000,"Barbados":6010052000,"Belarus":2200112000,"Belgium":2100056000,"Belize":6020084000,"Benin":5020204000,"Bermuda":6010060000,"Bhutan":3050064000,"Bolivia":6030068000,"Bosnia and Herzegovina":2200070000,"Botswana":5020072000,"Brazil":6030076000,"British Virgin Islands":6010092000,"Brunei":3060096000,"Bulgaria":2200100000,"Burkina Faso":5020854000,"Burundi":5020108000,"C?te d&#039Ivoire":5020384000,
            "Cambodia":3060116000,"Cameroon":5020120000,"Cape Verde":5020132000,"Cayman Islands":6010136000,"Central African Republic":5020140000,"Chad":5020148000,"Chile":6030152000,"China":3030156000,"Colombia":6030170000,"Comoros":5020174000,"Congo":5020178000,"Cook Islands":3070184000,"Costa Rica":6020188000,"Croatia":2200191000,"Cuba":6010192000,"Cyprus":2100196000,"Czech Republic":2200203000,"Democratic Republic of Congo":5020180000,"Denmark":2100208000,"Djibouti":5020262000,"Dominica":6010212000,"Dominican Republic":6010214000,"East Timor":3060626000,"Ecuador":6030218000,"Egypt":5010818000,"El Salvador":6020222000,"Equatorial Guinea":5020226000,"Eritrea":5020232000,"Estonia":2200233000,"Ethiopia":5020231000,"Falkland Islands":6030238000,"Federated state1s of Micronesia":3070583000,
            "Fiji":3070242000,"Finland":2100246000,"France":2100250000,"French Guiana":6030254000,"French Polynesia":3070258000,"Gabon":5020266000,"Gambia":5020270000,"Georgia":3040239000,"Germany":2100276000,"Ghana":5020288000,"Gibraltar":2100292000,"Greece":2100300000,"Greenland":2104304000,"Grenada":6010308000,"Guadeloupe":6010312000,"Guam":3070316000,"Guatemala":6020320000,"Guinea":5020324000,"Guinea-Bissau":5020624000,"Guyana":6030328000,"Haiti":6010332000,"Honduras":6020340000,"Hong Kong":3030344000,"Hungary":2200348000,"Iceland":2100352000,"India":3050356000,"Indonesia":3060360000,"Iran":3040364000,"Iraq":4000368000,"Ireland":2100372000,"Israel":4000376000,"Italy":2100380000,"Jamaica":6010388000,"Japan":3020392000,"Jordan":4000400000,"Kazakhstan":3040398000,"Kenya":5020404000,"Kiribati":3070296000,"Korea":3020410000,
            "Kuwait":4000414000,"Kyrgyzstan":3040417000,"Laos":3060418000,"Latvia":2200428000,"Lebanon":4000422000,"Lesotho":5020426000,"Liberia":5020430000,"Libya":5010434000,"Liechtenstein":2100438000,"Lithuania":2200440000,"Luxembourg":2100442000,"Macau":3030446000,"Macedonia":2200807000,"Madagascar":5020450000,"Malawi":5020454000,"Malaysia":3060458000,"Maldives":3050462000,"Mali":5020466000,"Malta":2200470000,"Marshall Islands":3070584000,"Martinique":6010474000,"Mauritania":5020478000,"Mauritius":5020480000,"Mexico":6020484000,"Moldova":2200498000,"Monaco":2100492000,"Mongolia":3020496000,"Montserrat":6010500000,"Morocco":5010504000,"Mozambique":5020508000,"Myanmar":3050104000,"Namibia":5020516000,"Nauru":3070520000,"Nepal":3050524000,"Netherlands Antilles":6010530000,"Netherlands":2100528000,"New Caledonia":3070540000,
            "New Zealand":3010554000,"Nicaragua":6020558000,"Niger":5020562000,"Nigeria":5020566000,"North Korea":3020408000,"Northern Mariana Islands":3070580000,"Norway":2100578000,"Oman":4000512000,"Pakistan":3050586000,"Palau":3070585000,"Panama":6020591000,"Papua New Guinea":3070598000,"Paraguay":6030600000,"Peru":6030604000,"Philippines":3060608000,"Poland":2200616000,"Portugal":2100620000,"Puerto Rico":6010630000,"Qatar":4000634000,"Romania":2200642000,"Russia":2200643000,"Rwanda":5020646000,"Saint Kitts & Nevis":6010659000,"Saint Lucia":6010662000,"Saint Vincent and The Grenadines":6010670000,"Samoa":3070882000,"San Marino":2100674000,"Saudi Arabia":4000682000,"Senegal":5020686000,"Serbia":2200688000,"Seychelles":5020690000,"Sierra Leone":5020694000,"Singapore":3060702000,"Slovakia":2200703000,"Slovenia":2200705000,"Solomon Islands":3070090000,
            "Somalia":5020706000,"South Africa":5020710000,"Spain":2100724000,"Sri Lanka":3050144000,"Sudan":5020736000,"Suriname":6030740000,"Swaziland":5020748000,"Sweden":2100752000,"Switzerland":2100756000,"Syria":4000760000,"Taiwan":3030158000,"Tajikistan":3040762000,"Tanzania":5020834000,"Thailand":3060764000,"Togo":5020768000,"Tonga":3070776000,"Trinidad and Tobago":6010780000,"Tunisia":5010504000,"Turkey":4000792000,"Turkmenistan":3040795000,"Turks and Caicos Islands":6010796000,"Tuvalu":3070798000,"US Virgin Islands":6010850000,"Uganda":5020800000,"Ukraine":2200804000,"United Arab Emirates":4000784000,"United Kingdom":2100826000,"Uruguay":6030858000,"Uzbekistan":3040860000,"Vanuatu":3070548000,"Vatican":2100336000,"Venezuela":6030862000,"Vietnam":3060704000,"Yemen":4000887000,"Zambia":5020894000,"Zimbabwe":5020716000
            };
            var selObj = document.createElement("select");
            var inpObj = document.createElement("input");
            var inpId = document.createAttribute("id");
            var inpName = document.createAttribute("name");
            var inpClass = document.createAttribute("class");
            var inpType = document.createAttribute("type");
            inpId.value="plain";
            inpName.value="state";
            inpType.value = "text";
            inpClass.value="plain_hidden";
            inpObj.setAttributeNode(inpId);
            inpObj.setAttributeNode(inpName);
            inpObj.setAttributeNode(inpType);
            selObj.name = _d.cid;
            selObj.id = _d.cid;
            selObj.onchange = function() {
                _t._(_d.pid) ? _t._('state1').removeChild(_t._(_d.pid)) : '';
                return _t._setcity(_d.cid, _d.pid, _d.box);
            };
            this._(_d.box).appendChild(selObj);
            _t._(_d.cid).className = "select_style3_100 m_right_10";//set country select class
            _t._("state1").appendChild(inpObj);
            selObj.options[0] = new Option("Select Country", "");
            var j = 1;
            for(i in provinces) {
                selObj.options[j] = new Option(i, provinces[i]);
                j++;
            }
            if(_d.pnum != "")
            {
                typeof(_d.cnum) == 'string' ? inpObj.setAttribute("value",_d.cnum) : '';
                inpObj.setAttributeNode(inpClass);
                _t._initprovcity(_d.cid, _d.pnum);
                return;
            }
            else{
                inpClass.value="plain_show";
                inpObj.setAttributeNode(inpClass);
            }
          },
    _: function (id){
                if(document.getElementById(id)){
                    return document.getElementById(id);
                }
         },
    _setcity: function (provinceid, cityid,boxid) {
                var province = parseFloat(this._(provinceid).value);
                switch (province) {
                    case 1000840000 :
                        var cityOptions = {
                                "Alabama":1000840301,"Alaska":1000840802,"Arizona":1000840704,"Arkansas":1000840305,"California":1000840806,"Colorado":1000840608,"Connecticut":1000840109,"Delaware":1000840210,"District of Columbia":1000840211,"Florida":1000840312,
                                "Georgia":1000840313,"Hawaii":1000840815,"Idaho":1000840616,"Illinois":1000840417,"Indiana":1000840418,"Iowa":1000840519,"Kansas":1000840520,"Kentucky":1000840421,"Louisiana":1000840322,"Maine":1000840123,"Maryland":1000840224,"Massachusetts":1000840125,
                                "Michigan":1000840426,"Minnesota":1000840427,"Mississippi":1000840328,"Missouri":1000840529,"Montana":1000840630,"Nebraska":1000840531,"Nevada":1000840732,"New Hampshire":1000840133,"New Jersey":1000840134,"New Mexico":1000840735,"New York":1000840136,
                                "North Carolina":1000840237,"North Dakota":1000840538,"Ohio":1000840439,"Oklahoma":1000840540,"Oregon":1000840841,"Pennsylvania":1000840442,"Rhode Island":1000840144,"South Carolina":1000840345,"South Dakota":1000840546,"Tennessee":1000840347,"Texas":1000840748,
                                "Utah":1000840649,"Vermont":1000840150,"Virginia":1000840251,"Washington":1000840853,"West Virginia":1000840254,"Wisconsin":1000840455,"Wyoming":1000840656
                                };
                         break;
                    case 1001124000 :
                        var cityOptions = {
                                "Alberta":1001124002,
                                "British Columbia":1001124008,
                                "Manitoba":1001124012,
                                "New Brunswick":1001124018,
                                "Newfoundland and Labrador":1001124022,
                                "Northwest Territories":1001124028,
                                "Nova Scotia":1001124032,
                                "Nunavut":1001124038,
                                "Ontario":1001124042,
                                "Prince Edward Island":1001124048,
                                "Quebec":1001124052,
                                "Saskatchewan":1001124058,
                                "Yukon Territory":1001124062
                        };
                        break;
                    default:
                        var cityOptions = null;
                        break;
                }
                if(cityOptions == null && this._(cityid))
                {
                    this._("state1").removeChild(this._(cityid));
                    this._("plain").setAttribute("name","state");
                    this._("plain").setAttribute("value","");
                    this._("plain").className = "plain_show";
                    return;
                }else if (cityOptions == null && !this._(cityid))
                {
                    this._("plain").setAttribute("name","state");
                    this._("plain").className = "plain_show";
                    return;
                }
                else if(!this._(cityid))
                {
                        var selObj = document.createElement("select");
                        selObj.name = cityid;
                        selObj.id = cityid;
                        this._("plain").className = "plain_hidden";
                        this._("plain").setAttribute("name","testing");
                        this._("state1").appendChild(selObj);
                        this._("state").className = "select_style3_100 m_right_10";
                }
                var cityObject = this._(cityid);
                var j = 0;
                for(i in cityOptions){
                    cityObject.options[j] = new Option(i,cityOptions[i]);
                    j++;
                }
                if(this.data.cnum != null && typeof(this.data.cnum) == "number"){
                    for(var i = 0; i < cityObject.options.length; i++) {
                            if (cityObject.options[i].value == this.data.cnum) {
                                cityObject.selectedIndex = i;
                                break;
                            }
                        }
                }
                },
    _initprovcity: function (provinceid, province) {
                        var provObject = this._(provinceid);
                        for(var i = 0; i < provObject.options.length; i++) {
                            if (provObject.options[i].value == province) {
                                provObject.selectedIndex = i;
                                break;
                            }
                        }
                        this._setcity(provinceid, this.data.pid);
                   }
}

<html>
<style type="text/css">
#selectbox{position:relative;}
.plain_show{display:block;}
.plain_hidden{display:none;}
</style>
<head>
<script type="text/javascript" src="selectcity_n.js"></script>
</head>
<body>
<div id="country"><label for="">Country</label></div>
dsafsafasfdasdfasdfasdfasd
<div id="state1"><label for="">State Province</label></div>
<script type="text/javascript">
    var countryProvcity = new countryProvcity({cid:'province', pid:'state', pnum:'2100246000', cnum:'', box:'country'});
    countryProvcity.init();
</script>
</body>
</html>

你可能感兴趣的:(JavaScript,prototype,J#,Gmail)