根据国家/地区更改 select 中的选项

Changing options in select based from country

我有一个包含 2 selects(国家和城市)的添加客户页面。当我选择Country和选择USA时,第二个select会有美国城市的选项。如果我选择 Philippines 选项将是菲律宾的城市,与 Canada 相同,等等

我的代码是这样的:

<select class="form-control" name="country" required>
          <option selected disabled>*Select Country </option>
          <option>USA</option>
          <option>Philippines</option>
          <option>Canada</option>

</select> &nbsp;&nbsp;&nbsp;

       //If 'USA' was selected the select options values will be this like.

        <select class="form-control" name="loc" required>
          <option selected disabled>*State / City</option>
          <option></option>
          <option>California</option>
          <option>New York</option>
          <option>New Jersey</option>
          <option>Illinois</option>
          <option>Others</option>
        </select>

     //else If 'Philippines' was selected the select options values will be this like.

        <select class="form-control" name="loc" required>
           <option selected disabled>*State / City</option>
          <option></option>
          <option>Manila</option>
          <option>Quezon City</option>
          <option>Makati</option>
          <option>Cebu</option>
          <option>Davao</option>
          <option>Others</option>



     //else If 'Canada' was selected the select options values will be this like.

        <select class="form-control" name="loc" required>
           <option selected disabled>*State / City</option>
          <option></option>
          <option>Toronto</option>
          <option>Vancouver</option>
          <option>Others</option>
        </select>

您展示的 fiddle 工作正常。只需将此行包含在您的 <head> 标签中...

<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>

注意:根据 OP 评论删除了原始答案。

为了轻松实现你想要的,你可以使用jQuery,你可以下载here

然后我为您的 <select> 字段添加了 id 标签,为您的选项添加了 value 标签。

<select class="form-control" name="country" id="country" required>
  <option selected disabled>*Select Country </option>
  <option option="USA">USA</option>
  <option option="Philippines">Philippines</option>
  <option option="Canada">Canada</option>
</select>
<select class="form-control" name="loc" id="loc" required>
<!-- REST OF OTHER CODES -->

然后创建脚本:

<script src="jquery-1.9.1.min.js"></script><!-- REPLACE NECESSARY JQUERY FILE DEPENDING ON THE VERSION YOU HAVE DOWNLOADED -->

<!-- START CREATING YOUR SCRIPT -->
<script type="text/javascript">

  $(document).ready(function(){

    $("#country").change(function(){
       var country = $(this).val();
       var usa = '<option>California</option><option>New York</option><option>New Jersey</option><option>Illinois</option><option>Others</option>';
       var phi = '<option>Manila</option><option>Quezon City</option><option>Makati</option><option>Cebu</option><option>Davao</option><option>Others</option>';
       var can = '<option>Toronto</option><option>Vancouver</option>'; 
       var other = '<option selected disabled>*State / City</option>';

       if(country == "USA"){
         $("#loc").empty().append(usa);
       }
       else if(country == "Philippines"){
         $("#loc").empty().append(phi);
       }
       else if(country == "Canada"){
         $("#loc").empty().append(can);
       }
       else {
         $("#loc").empty().append(other);  
       }
    });

  });

</script>

看看这个JSFiddle

知道你的表单是静态的,我不得不放弃我的第一个答案。