Last active
December 14, 2015 21:48
-
-
Save jonathancg90/5153180 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function(){ | |
console.log('hello'); | |
var country = $('#id_country').change(setStates); | |
var state = $('#id_state').change(setCity); | |
//Saber que formulario estamos utilizando | |
if ($('#id_city').length){ | |
var city = $('#id_city'); | |
} else { | |
var city = $('#id_id'); | |
} | |
//Asignar clase de chozen | |
state.addClass('chzn-select'); | |
city.addClass('chzn-select'); | |
//Validacion despues de haber realziado una busqueda | |
if(state.find('option:selected').val().length !== 0 ){ | |
state.chosen(); | |
} | |
if(city.find('option:selected').val().length !== 0 ){ | |
city.chosen(); | |
} | |
//Cargar estados de los paises | |
function setStates(){ | |
var value = $(this).find('option:selected').val(); | |
var url = url_states_by_country; | |
//Selecciono opcion valida | |
if (value !== undefined && value.length !== 0) { | |
url=url.replace("0",value); | |
} else { | |
state.empty(); | |
city.empty(); | |
state.trigger("liszt:updated"); | |
city.trigger("liszt:updated"); | |
return; | |
} | |
$.ajax({ | |
url: url, | |
dataType:'json', | |
success : function(data){ | |
state.trigger("liszt:updated"); | |
state.empty(); | |
state.append("<option value=''>--------------</option>"); | |
city.empty(); | |
$.each(data['result'],function(i, value){ | |
state.append("<option value="+value[0]+">"+value[1]+"</option>"); | |
}); | |
}, | |
complete : function(){ | |
//Actualizacion e inicializacion de chosen | |
state.trigger("liszt:updated"); | |
city.trigger("liszt:updated"); | |
state.chosen(); | |
} | |
}); | |
}; | |
//Cargar las ciudades de el estado seleccionado | |
function setCity(){ | |
console.log("entro"); | |
city.trigger("liszt:updated"); | |
var value = $(this).find('option:selected').val(); | |
var url = url_city_by_states; | |
//Selecciono opcion valida | |
if (value !== undefined) { | |
url=url.replace("0",value); | |
} | |
$.ajax({ | |
url : url, | |
dataType : 'json', | |
success : function(data){ | |
city.empty(); | |
city.append("<option value=''></option>"); | |
$.each(data['cities'],function(i, value){ | |
city.append("<option value="+value['id']+">"+value['name']+"</option>"); | |
}); | |
}, | |
complete : function(){ | |
//Actualizacion e inicializacion de chosen | |
city.trigger("liszt:updated"); | |
city.chosen(); | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment