Issue
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>
//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>
Solution
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.
Answered By - Logan Wayne
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.