Value of Input to an Array

0

I have an input filled by foreach (several values):

@foreach($detalles as $det)
                    <tr>            
                        <input type="hidden" name="id_detalle[]" value="{{ $det->id_detalle }}">
                        <td><input style=" width : 60px; border: none " type="number" name="cantidad[]" id="cantidad[]" value="{{ $det->cantidad }}"></td>
                        <td><input style=" width : 600px; border: none " type="text" readonly name="descripcion[]" id="descripcion[]" value="{{ $det->descripcion }}"></td>
                        <td><input style=" width : 90px; border: none " type="number" step="0.01" name="v_unitario[]" id="v_unitario" value="{{ $det->v_unitario }}"></td>
                        <td><input style=" width : 90px; border: none " type="number" readonly id="v_parcial" name="v_parcial[]" value="{{ $det->v_parcial }}"></td>
                    </tr>
                @endforeach

How can I pass the input values id="cantidad[]" to an array in javascript?

    
asked by Jonathan Vincent 15.06.2018 в 20:50
source

1 answer

0

You can aprobechar that all have the same name and then you have all the ones that match the arrangement and you store it in an array

<script>
    //Obtengo todos los campos con el nombre cantidad[]
    var cantidad = document.getElementsByName("cantidad[]");
    //Creo el arreglo donde almaceno sus valores
    var arreglo = [];
    //Recorro todos los nodos que encontre que coinciden con ese nombre
    for(var i=0;i<cantidad.length;i++){
        //Añado el valor que contienen los campos
        arreglo.push(cantidad[i].value);
    }
    console.log(arreglo);
</script>
    
answered by 15.06.2018 в 22:11