I have a select that in a file js is serving me for different things like a change so that I list those cities that depend on the id of the country, it turns out that now I need is that when you click on the option (en el select del pais)
, example in the country MEXICO I take that id and keep it in a global variable ...
for then to an input that is inside a form to give that value and save it together with the city that is entered, that is to say that when obtaining that value something like the following can be saved:
name:"id_pais"
value: "5"
name:"nombre_ciudad"
value : "monterrey"
I hope you have made me understand and I would appreciate your collaboration.
THIS IS THE CODE I'M USING UNTIL NOW
var IdPais;
$(document).ready(function()
{
function getPais()
{
return $.ajax({ url: 'configuracion/lista_pais', type: 'GET', dataType: 'json'});
}
getPais()
.done(function(respuesta)
{
try
{
var resultado = respuesta;
for(var i = resultado.length - 1; i >= 0; i--)
{
// AQUI HAGO EL OPTION PARA LISTAR LOS PAISES
}
}
catch(e)
{
console.info(e);
}
});
// ACÁ HAGO EL CHANGE PARA LISTAR EN UNA TABLA LAS CIUDADES
$('#paises').change(function(event)
{
event.preventDefault();
//aqui supuestamente declaro la variable global
IdPais = $(this).val();
$.ajax({url: 'configuracion/listar_ciudades', type: 'POST', dataType: 'json', data: {'id_pais': IdPais},})
.done(function(respuesta)
{
try
{
var resultado = respuesta;
for(var i = resultado.length - 1; i >= 0; i--)
{
var rowNode = ciudades
.row.add([
resultado[i].ciudad_id,
resultado[i].ciudad_nombren,
'<center><button id="editar" class="btn btn-info" style="background: orange; border-color: orange; width: 30px; height: 30px; text-align: center; padding: 6px 0; font-size: 12px; line-height: 1.428571429; border-radius: 15px;" title="editar"><span class="glyphicon glyphicon-pencil"</span></button></center>',
])
.draw()
.node();
}
}
catch(e)
{
console.info(e);
}
})
.fail(function(){
console.log("error");
});
});
$('#id_pais').val(IdPais); // Es aqui donde se deberia de guardar el id del pais
HTML
<form>
<input type="hidden" name="id_pais" id="id_pais">
<input type="text" name="nombre_ciudad" id="nombre_ciudad">
<input type="submit" value="guardar">
</form>