我有一个看起来像
的对象var countryRegions =
{
"config": {
"show_all_regions": true,
"regions_required": ["AT", "CA", "EE", "FI", "FR", "DE", "HK", "LV", "LT", "RO", "ES", "CH", "US"]
},
"HK": {
"494": {
"code": "FN",
"name": "Hong Kong"
}
},
"CH": {
"104": {
"code": "AG",
"name": "Aargau"
},
"106": {
"code": "AR",
"name": "Appenzell Ausserrhoden"
},
"105": {
"code": "AI",
"name": "Appenzell Innerrhoden"
},
"108": {
"code": "BL",
"name": "Basel-Landschaft"
},
"109": {
"code": "BS",
"name": "Basel-Stadt"
},
"107": {
"code": "BE",
"name": "Bern"
},
"110": {
"code": "FR",
"name": "Freiburg"
},
"111": {
"code": "GE",
"name": "Genf"
},
"112": {
"code": "GL",
"name": "Glarus"
},
"113": {
"code": "GR",
"name": "Graub\u00fcnden"
},
"114": {
"code": "JU",
"name": "Jura"
},
"115": {
"code": "LU",
"name": "Luzern"
},
"116": {
"code": "NE",
"name": "Neuenburg"
},
"117": {
"code": "NW",
"name": "Nidwalden"
},
"118": {
"code": "OW",
"name": "Obwalden"
},
"120": {
"code": "SH",
"name": "Schaffhausen"
},
"122": {
"code": "SZ",
"name": "Schwyz"
},
"121": {
"code": "SO",
"name": "Solothurn"
},
"119": {
"code": "SG",
"name": "St. Gallen"
},
"124": {
"code": "TI",
"name": "Tessin"
},
"123": {
"code": "TG",
"name": "Thurgau"
},
"125": {
"code": "UR",
"name": "Uri"
},
"126": {
"code": "VD",
"name": "Waadt"
},
"127": {
"code": "VS",
"name": "Wallis"
},
"128": {
"code": "ZG",
"name": "Zug"
},
"129": {
"code": "ZH",
"name": "Z\u00fcrich"
}
},
"US": {
"1": {
"code": "AL",
"name": "Alabama"
},
"2": {
"code": "AK",
"name": "Alaska"
},
"3": {
"code": "AS",
"name": "American Samoa"
},
"4": {
"code": "AZ",
"name": "Arizona"
},
"5": {
"code": "AR",
"name": "Arkansas"
},
"6": {
"code": "AE",
"name": "Armed Forces Africa"
},
"7": {
"code": "AA",
"name": "Armed Forces Americas"
},
"8": {
"code": "AE",
"name": "Armed Forces Canada"
},
"9": {
"code": "AE",
"name": "Armed Forces Europe"
},
"10": {
"code": "AE",
"name": "Armed Forces Middle East"
},
"11": {
"code": "AP",
"name": "Armed Forces Pacific"
},
"12": {
"code": "CA",
"name": "California"
},
"13": {
"code": "CO",
"name": "Colorado"
},
"14": {
"code": "CT",
"name": "Connecticut"
},
"15": {
"code": "DE",
"name": "Delaware"
},
"16": {
"code": "DC",
"name": "District of Columbia"
},
"17": {
"code": "FM",
"name": "Federated States Of Micronesia"
},
"18": {
"code": "FL",
"name": "Florida"
},
"19": {
"code": "GA",
"name": "Georgia"
},
"20": {
"code": "GU",
"name": "Guam"
},
"21": {
"code": "HI",
"name": "Hawaii"
},
"22": {
"code": "ID",
"name": "Idaho"
},
"23": {
"code": "IL",
"name": "Illinois"
},
"24": {
"code": "IN",
"name": "Indiana"
},
"25": {
"code": "IA",
"name": "Iowa"
},
"26": {
"code": "KS",
"name": "Kansas"
},
"27": {
"code": "KY",
"name": "Kentucky"
},
"28": {
"code": "LA",
"name": "Louisiana"
},
"29": {
"code": "ME",
"name": "Maine"
},
"30": {
"code": "MH",
"name": "Marshall Islands"
},
"31": {
"code": "MD",
"name": "Maryland"
},
"32": {
"code": "MA",
"name": "Massachusetts"
},
"33": {
"code": "MI",
"name": "Michigan"
},
"34": {
"code": "MN",
"name": "Minnesota"
},
"35": {
"code": "MS",
"name": "Mississippi"
},
"36": {
"code": "MO",
"name": "Missouri"
},
"37": {
"code": "MT",
"name": "Montana"
},
"38": {
"code": "NE",
"name": "Nebraska"
},
"39": {
"code": "NV",
"name": "Nevada"
},
"40": {
"code": "NH",
"name": "New Hampshire"
},
"41": {
"code": "NJ",
"name": "New Jersey"
},
"42": {
"code": "NM",
"name": "New Mexico"
},
"43": {
"code": "NY",
"name": "New York"
},
"44": {
"code": "NC",
"name": "North Carolina"
},
"45": {
"code": "ND",
"name": "North Dakota"
},
"46": {
"code": "MP",
"name": "Northern Mariana Islands"
},
"47": {
"code": "OH",
"name": "Ohio"
},
"48": {
"code": "OK",
"name": "Oklahoma"
},
"49": {
"code": "OR",
"name": "Oregon"
},
"50": {
"code": "PW",
"name": "Palau"
},
"51": {
"code": "PA",
"name": "Pennsylvania"
},
"52": {
"code": "PR",
"name": "Puerto Rico"
},
"53": {
"code": "RI",
"name": "Rhode Island"
},
"54": {
"code": "SC",
"name": "South Carolina"
},
"55": {
"code": "SD",
"name": "South Dakota"
},
"56": {
"code": "TN",
"name": "Tennessee"
},
"57": {
"code": "TX",
"name": "Texas"
},
"58": {
"code": "UT",
"name": "Utah"
},
"59": {
"code": "VT",
"name": "Vermont"
},
"60": {
"code": "VI",
"name": "Virgin Islands"
},
"61": {
"code": "VA",
"name": "Virginia"
},
"62": {
"code": "WA",
"name": "Washington"
},
"63": {
"code": "WV",
"name": "West Virginia"
},
"64": {
"code": "WI",
"name": "Wisconsin"
},
"65": {
"code": "WY",
"name": "Wyoming"
}
},
"CA": {
"66": {
"code": "AB",
"name": "Alberta"
},
"67": {
"code": "BC",
"name": "British Columbia"
},
"68": {
"code": "MB",
"name": "Manitoba"
},
"70": {
"code": "NB",
"name": "New Brunswick"
},
"69": {
"code": "NL",
"name": "Newfoundland and Labrador"
},
"72": {
"code": "NT",
"name": "Northwest Territories"
},
"71": {
"code": "NS",
"name": "Nova Scotia"
},
"73": {
"code": "NU",
"name": "Nunavut"
},
"74": {
"code": "ON",
"name": "Ontario"
},
"75": {
"code": "PE",
"name": "Prince Edward Island"
},
"76": {
"code": "QC",
"name": "Quebec"
},
"77": {
"code": "SK",
"name": "Saskatchewan"
},
"78": {
"code": "YT",
"name": "Yukon Territory"
}
},
"AT": {
"102": {
"code": "BL",
"name": "Burgenland"
},
"99": {
"code": "KN",
"name": "K\u00e4rnten"
},
"96": {
"code": "NO",
"name": "Nieder\u00f6sterreich"
},
"97": {
"code": "OO",
"name": "Ober\u00f6sterreich"
},
"98": {
"code": "SB",
"name": "Salzburg"
},
"100": {
"code": "ST",
"name": "Steiermark"
},
"101": {
"code": "TI",
"name": "Tirol"
},
"103": {
"code": "VB",
"name": "Vorarlberg"
},
"95": {
"code": "WI",
"name": "Wien"
}
}
}
我有一个看起来像的选择列表:
<select id="billing:region_id">
<option value="1"> Alabama </option>
<option value="2"> Arizona </option>
etc....
</select>
我试图遍历选项元素并将其相应的国家/地区代码作为id附加到元素上,但是当我像这样循环时,它会以未定义的形式返回:
jQuery("#billing\\:region_id option").each(function(k,v){
v.id = countryRegions['US'][Number(v.value)]['code'];
});
并且这些都不起作用
v.id = countryRegions['US'][Number(v.value)]['code'];
v.id = countryRegions['US'][Number(v.value)].code;
v.id = countryRegions.US[Number(v.value)]['code'];
但是当我说
时 v.id = countryRegions['US'][1]['code'];
这很好用,但是每个选项元素都会获得第一个目标代码。
我在这里做错了什么?
答案 0 :(得分:1)
在尝试使用之前,先检查数组中是否存在该值。
jQuery("#billing\\:region_id option").each(function(k,v){
if (countryRegion.US[v.value]) {
v.id = countryRegion.US[v.value].code;
}
});