实际上,我正在为两个国家(加拿大和美国)的每个州创建一个下拉列表,以适应我需要为客户使用的API。其实,我对获取“国家”的价值没有任何疑问。但是,一旦通过if和for循环,就不会再发布更多信息了。
function stateByCountry(){
var countryList = document.getElementById("country");
var strCountry = countryList.options[countryList.selectedIndex].value;
console.log(strCountry);
var stateSelect = document.getElementById("state");
var caState = ["Alberta","British Columbia","Manitoba","New Brunswick","Newfoundland and Labrador","Nova Scotia","Northwest Territories","Nunavut","Ontario","Prince Edward Island","Quebec","Saskatchewan","Yukon"];
var caStateValues = ["AB","BC","MB","NB","NL","NS","NT","NU","ON","PE","QC","SK","YT"];
var usState = [];
var usStateValues = [];
if (strCountry == "CA") {
for (var i = 0; i < caState.lenght; i++) {
var state = caState[i];
console.log(state);
var stateValue = caStateValues[i];
var stateOption = document.createElement("option");
stateOption.textContent = state;
stateOption.value = stateValue;
stateSelect.appendChild(stateOption);
console.log(state);
}
}
if (strCountry == "US") {
}
}