How to give value to input with a global variable jquery?

1

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>
    
asked by JDavid 21.03.2017 в 23:22
source

1 answer

1

You would simply add this line of code to assign the SELECT value to the HIDDEN BOX:

First you have a alert($this.val()); to see if it shows you the value of the select

Then we had it like this:

// here I supposedly declare the global variable

   IdPais = $(this).val();

// Instead of a global variable we assign it to the text box every time it changes

   var valor=$this.val();
   $("#id_pais").val(valor);

ASSUMING THAT: everything is in the same form, sorry for the poorly structured response I do not have a mouse and doing it on a pure keyboard is difficult It is not necessary to make it global, because if you get it you pass it directly to your text box and wuala miracles of JQuery

I'll give you this example:

<script>
$(document).ready(function(){
    $("select[name=numeros]").change(function(){
            alert($('select[name=numeros]').val());
            $('input[name=valor]').val($(this).val());
        });
});
</script>

<html>
<select name="numeros">
<option value=1>UNO</option>
<option value=2>DOS</option>
<option value=3>TRES</option>
</select>

<input type="text" name="valor">Valor

</html>
    
answered by 21.03.2017 / 23:29
source