I'm trying to get data from API to be displayed in Dropdown List, even though the parent Dropdown is displaying the data, the rest of the Dropdown List doesn't show anything when i select a data from parent Dropdown.
Here is my Script:
<script type="text/javascript">
$('#province').change(function(){
var provinceID = $(this).val();
if(provinceID){
$.ajax({
type:"GET",
url:"http://dev.example.com/api/districts_list?province="+provinceID,
success:function(res){
if(res){
$("#districts").empty();
$("#districts").append('<option>Select</option>');
$.each(res,function(key,value){
$("#districts").append('<option value="'+key+'">'+value+'</option>');
});
}else{
$("#districts").empty();
}
}
});
}else{
$("#districts").empty();
$("#subdistrict").empty();
}
});
$('#districts').on('change',function(){
var districtsID = $(this).val();
if(districtsID ){
$.ajax({
type:"GET",
url:"http://dev.example.com/api/subdistrict_list?kab="+districtsID ,
success:function(res){
if(res){
$("#subdistrict").empty();
$.each(res,function(key,value){
$("#subdistrict").append('<option value="'+key+'">'+value+'</option>');
});
}else{
$("#subdistrict").empty();
}
}
});
}else{
$("#subdistrict").empty();
}
});
</script>
And here is my view:
<div class="form-row">
<div class="form-group col-md-4">
<label for="province">Province</label>
<select id="province" name="province" class="form-control">
<option selected>Choose...</option>
@foreach ($provinces as $key)
<option value="{{ $key->province_id }}">{{ $key->province }}</option>
@endforeach
</select>
</div>
<div class="form-group col-md-4">
<label for="districts">Districts</label>
<select id="districts" name="districts" class="form-control">
</select>
</div>
<div class="form-group col-md-4">
<label for="subdistrict">Sub-district</label>
<select id="subdistrict" name="subdistrict" class="form-control">
</select>
</div>
</div>
I don't understand where i was wrong, do you have any idea?