Name not defined when I walk with each

0

I am collecting data in inputs with a each , when I try to add it in a variable it tells me that the name is not defined, it seems I have a syntax error.

function guardar_testimonio(){
    $("#infotestis").each(function(){
        var nombre = $("#infotestis").parent().find("input").eq(0).val();
        var testimoniouno= $("#infotestis").parent().find("input").eq(1).val();
        var testimoniodos= $("#infotestis").parent().find("input").eq(2).val();;
        var compania= $("#infotestis").parent().find("input").eq(3).val();;
        var lugar= $("#infotestis").parent().find("input").eq(4).val();;
        console.log(nombre+" "+testimoniouno+" "+testimoniodos+" "+compania+" "+lugar);
    });
 
    var datos_formulario = "nombres=" + nombre;
        // "&testimoniouno="+testimoniouno+ "&testimoniodos="+testimoniodos +"&compania="+ compania + "&lugar="+ lugar;

        $.ajax({
            type: 'POST',
            url: '../dashmin/php/agregar.php',
            data: datos_formulario,
            dataType: 'json',
            beforeSend: function (objeto) {

            },
            success: function (json) {     


            },
            error: function (e) {

            },
            complete: function (objeto, exito, error) {

            }
        });

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="infotestis">
      <h5>Nombre:</h5> <input class="form-control" type="text" value="Alejando Sallustro">
       <h5>Testimonio:</h5> <input class="form-control" type="text" value="Una inversión acorde"><input class="form-control" type="text" value="a estos tiempos">
       <h5>Compañia:</h5> <input class="form-control" type="text" value="CEO, Sallustro &amp; Cía.">
       <h5>Lugar:</h5> <input class="form-control" type="text" value="Asuncion">
       <h5>Perfil:</h5> <img src="" alt="">
        <button onclick="guardar_testimonio()" class="btn btn-primary">Guardar<div class="ripple-container"></div></button>
                                                
</div>
    
asked by Cesar 29.01.2018 в 20:51
source

2 answers

3

In your case your each is not doing anything since you are accessing the values of each input directly, simply remove it.

It should be noted that the .parent() method is not necessary for your selector since you are selecting elements that are direct children of #infotestis

function guardar_testimonio(){
        var nombre = $("#infotestis").find("input").eq(0).val();
        var testimoniouno= $("#infotestis").find("input").eq(1).val();
        var testimoniodos= $("#infotestis").find("input").eq(2).val();
        var compania= $("#infotestis").find("input").eq(3).val();
        var lugar= $("#infotestis").find("input").eq(4).val();
        console.log(nombre+" "+testimoniouno+" "+testimoniodos+" "+compania+" "+lugar);
 
    var datos_formulario = "nombres=" + nombre;
        // "&testimoniouno="+testimoniouno+ "&testimoniodos="+testimoniodos +"&compania="+ compania + "&lugar="+ lugar;

        $.ajax({
            type: 'POST',
            url: '../dashmin/php/agregar.php',
            data: datos_formulario,
            dataType: 'json',
            beforeSend: function (objeto) {

            },
            success: function (json) {     


            },
            error: function (e) {

            },
            complete: function (objeto, exito, error) {

            }
        });

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="infotestis">
                                                <h5>Nombre:</h5> <input class="form-control" type="text" value="Alejando Sallustro">
                                                <h5>Testimonio:</h5> <input class="form-control" type="text" value="Una inversión acorde"><input class="form-control" type="text" value="a estos tiempos">
                                                <h5>Compañia:</h5> <input class="form-control" type="text" value="CEO, Sallustro &amp; Cía.">
                                                <h5>Lugar:</h5> <input class="form-control" type="text" value="Asuncion">
                                                <h5>Perfil:</h5> <img src="" alt="">
                                                <button onclick="guardar_testimonio()" class="btn btn-primary">Guardar<div class="ripple-container"></div></button>
                                                
                                            </div>
    
answered by 29.01.2018 / 20:57
source
1

In javascript, the scope of the variables are defined by the function in which they are defined:

$("#infotestis").each(function(){
    var scope = 1;
    console.log(scope); //imprime 1
});
console.log(scope); //imprime undefined

On the other hand, the jQuery each(function(){...}) method executes a function for each element that complies with the selector that precedes it. In your case it is enough to execute the body of the function without each :

function guardar_testimonio(){
    var nombre = $("#infotestis").parent().find("input").eq(0).val();
    var testimoniouno= $("#infotestis").parent().find("input").eq(1).val();
    var testimoniodos= $("#infotestis").parent().find("input").eq(2).val();;
    var compania= $("#infotestis").parent().find("input").eq(3).val();;
    var lugar= $("#infotestis").parent().find("input").eq(4).val();;
    console.log(nombre+" "+testimoniouno+" "+testimoniodos+" "+compania+" "+lugar);

    var datos_formulario = "nombres=" + nombre;
        // "&testimoniouno="+testimoniouno+ "&testimoniodos="+testimoniodos +"&compania="+ compania + "&lugar="+ lugar;

        $.ajax({
            type: 'POST',
            url: '../dashmin/php/agregar.php',
            data: datos_formulario,
            dataType: 'json',
            beforeSend: function (objeto) {

            },
            success: function (json) {     


            },
            error: function (e) {

            },
            complete: function (objeto, exito, error) {

            }
        });

}
    
answered by 29.01.2018 в 21:08