简体   繁体   中英

Changing options in select based from country

I have an add customer page with 2 selects (Country and City). When I choose Country and choose USA , the 2nd select will have the options of cities in USA. If I choose Philippines the options will be the cities in the Philippines, same with Canada , etc.

My Code is like this:

<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>

Kumusta Edmhar,

The fiddle you have shown works fine. Just include this line in your <head> tags...

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

Happy Coding !

NOTE: Scrapped original answer based on OP comments.

In order to easily achieve what you want, you can use jQuery which you can download here .

Then I added id tags for your <select> fields and value tags for your options.

<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 -->

Then create the script:

<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>

Take a look at this JSFiddle .

I have to scrap my first answer knowing that your form is a static one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM