Skip to content

Instantly share code, notes, and snippets.

@wilbert
Created April 7, 2014 20:26
Show Gist options
  • Save wilbert/10045554 to your computer and use it in GitHub Desktop.
Save wilbert/10045554 to your computer and use it in GitHub Desktop.
Fill a combox field from a json with cities and neighborhoods when state select changes
jQuery(document).ready(function($){
$("#state_field_id").change(function(e){
$.ajax({
url: "/addresses/cities",
data: {
state_id: $(this).val()
}
}).done(function(data){
$.each(data, function(index, item){
$("#city_field_id").append($('<option />').attr("value", item.id).html(item.name));
})
});
});
$("#city_field_id").change(function(e){
$.ajax({
url: "/addresses/neighborhoods",
data: {
city_id: $(this).val()
}
}).done(function(data){
$.each(data, function(index, item){
$("#neighborhood_field_id").append($('<option />').attr("value", item.id).html(item.name));
})
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment