根据国家/地区选择使用值填充状态

Populate States Based on Country Selection with Value

我有下面的代码,我希望第二个 select 元素根据国家 selection 填充。因此,如果用户 selects 美国,它将填充第一个数组,如 Aalaska 将是文本,AK 将是值,Alabama 将是文本,AL 将是值,依此类推...

<p>
    <select id="country" name ="country">
        <option value="select">Select country</option>
        <option value="US">United States</option>
        <option value="CA">Canada</option>
    </select>
</p>
<p>
    <select name ="state" id ="state">
        <option value="select">Select state/province</option>
    </select>
</p>
<script>
var us_states = {"AK":"Alaska", "AL":"Alabama", "AR":"Arkansas", "AS":"American Samoa", "AZ":"Arizona", "CA":"California", "CO":"Colorado", "CT":"Connecticut", "DC":"District of Columbia", "DE":"Delaware", "FL":"Florida", "FM":"Federated States of Micronesia", "GA":"Georgia", "GU":"Guam", "HI":"Hawaii", "IA":"Iowa", "ID":"Idaho", "IL":"Illinois", "IN":"Indiana", "KS":"Kansas", "KY":"Kentucky", "LA":"Louisiana", "MA":"Massachusetts", "MD":"Maryland", "ME":"Maine", "MH":"Marshall Islands", "MI":"Michigan", "MN":"Minnesota", "MO":"Missouri", "MP":"Northern Mariana Islands", "MS":"Mississippi", "MT":"Montana", "NC":"North Carolina", "ND":"North Dakota", "NE":"Nebraska", "NH":"New Hampshire", "NJ":"New Jersey", "NM":"New Mexico", "NV":"Nevada", "NY":"New York", "OH":"Ohio", "OK":"Oklahoma", "OR":"Oregon", "PA":"Pennsylvania", "PR":"Puerto Rico", "PW":"Palau", "RI":"Rhode Island", "SC":"South Carolina", "SD":"South Dakota", "TN":"Tennessee", "TX":"Texas", "UT":"Utah", "VA":"Virginia", "VI":"Virgin Islands", "VT":"Vermont", "WA":"Washington", "WI":"Wisconsin", "WV":"West Virginia", "WY":"Wyoming"};
var ca_states = {"AB":"Alberta", "BC":"British Columbia", "MB":"Manitoba", "NB":"New Brunswick", "NL":"Newfoundland", "NS":"Nova Scotia", "NT":"Northwest Territories", "NU":"Nunavut", "ON":"Ontario", "PE":"Prince Edward Island", "QC":"Quebec", "SK":"Saskatchewan", "YT":"Yukon"};
</script>

如果您将列表放在一个对象中,会更容易。然后您可以使用 select 选项的值来选择列表。然后循环并追加:

var stateLists = {
     us_states: {"AK":"Alaska", "AL":"Alabama", "AR":"Arkansas", "AS":"American Samoa", "AZ":"Arizona", "CA":"California", "CO":"Colorado", "CT":"Connecticut", "DC":"District of Columbia", "DE":"Delaware", "FL":"Florida", "FM":"Federated States of Micronesia", "GA":"Georgia", "GU":"Guam", "HI":"Hawaii", "IA":"Iowa", "ID":"Idaho", "IL":"Illinois", "IN":"Indiana", "KS":"Kansas", "KY":"Kentucky", "LA":"Louisiana", "MA":"Massachusetts", "MD":"Maryland", "ME":"Maine", "MH":"Marshall Islands", "MI":"Michigan", "MN":"Minnesota", "MO":"Missouri", "MP":"Northern Mariana Islands", "MS":"Mississippi", "MT":"Montana", "NC":"North Carolina", "ND":"North Dakota", "NE":"Nebraska", "NH":"New Hampshire", "NJ":"New Jersey", "NM":"New Mexico", "NV":"Nevada", "NY":"New York", "OH":"Ohio", "OK":"Oklahoma", "OR":"Oregon", "PA":"Pennsylvania", "PR":"Puerto Rico", "PW":"Palau", "RI":"Rhode Island", "SC":"South Carolina", "SD":"South Dakota", "TN":"Tennessee", "TX":"Texas", "UT":"Utah", "VA":"Virginia", "VI":"Virgin Islands", "VT":"Vermont", "WA":"Washington", "WI":"Wisconsin", "WV":"West Virginia", "WY":"Wyoming"}
   , ca_states:  {"AB":"Alberta", "BC":"British Columbia", "MB":"Manitoba", "NB":"New Brunswick", "NL":"Newfoundland", "NS":"Nova Scotia", "NT":"Northwest Territories", "NU":"Nunavut", "ON":"Ontario", "PE":"Prince Edward Island", "QC":"Quebec", "SK":"Saskatchewan", "YT":"Yukon"}
}

var select = document.getElementById('state')

function setStates(el) {
  if (el.value) {
    select.options.length = 0
    var list = stateLists[el.value]
    for (key in list) {
      var opt = document.createElement('option');
      opt.value = key;
      opt.innerHTML = list[key];
      select.appendChild(opt);
    }
  }
}
<p>
  <select id="country" name="country" onchange=setStates(this)>
            <option value="">Select country</option>
            <option value="us_states">United States</option>
            <option value="ca_states">Canada</option>
        </select>
</p>
<p>
  <select name="state" id="state">
            <option value="select">Select state/province</option>
        </select>
</p>

希望这个答案能有所帮助,虽然方法有点长

var selectus = document.getElementById("us_state");
for(indexus in us_states) {
    selectus.options[selectus.options.length] = new 
Option(us_states[indexus], indexus);
}

var selectca = document.getElementById("ca_state");
for(indexca in ca_states) {
    selectca.options[selectca.options.length] = new 
Option(ca_states[indexca], indexca);
}

FIDDLE