help with a javascript for

0

Good day, I'm trying to put a value in a normal amount input, click on a button and I get the number of fields that says in quantity.

I'm doing it this way but I only get 2 fields so I put 4.

var cantidad = document.getElementsByName("cant");
function agregarDetalle(idseriales,serial){
for (var i = 0; i <=cantidad.length; i++) {
var fila='<tr class="filas" id="fila'+cont+'">'+
        '<td><button type="button" class="btn btn-danger" onclick="eliminarDetalle('+cont+')">X</button></td>'+
        '<td><input type="text" name="serial[]" value="" id="serial[]"></input></td>'+
        '<td><button  type="button" id="agregarArt" class="btn btn-primary" onclick="agregarDetalle()"><span class="fa fa-plus"></span></button><td>'+
        '<td><input type="hidden" name="idseriales[]" id="idseriales[]" value="'+idseriales+'"></input></td>'+

        '</tr>';

        cont++;
        detalles=detalles+1;
        $('#detalles').append(fila); 
    }
}

I would appreciate your comments since I do not know how to do so that I take the value that I put in the input

    
asked by Sebastian Lozano 27.08.2018 в 19:19
source

1 answer

0

It is that you are not taking the value of the field and you are doing the for for the length of the field when it should be for the value. With the addFila function I can send you an object with the properties you want to use.

$("#add").on("click", function(){
  $("#detalles").empty();
  for (var i = 0; i <= $("#cant").val() - 1; i++) {
    addFila({
      fila: i
    });
  }
});

function addFila(row) {
    var newFila = '<tr class="filas" id="fila${row.fila}">' +
      '<td><button type="button" class="btn btn-danger" 
            onclick="eliminarDetalle(${row.fila})">X</button></td>' +
      '<td><input type="text" id="serial${row.fila}" 
            value="serial${row.fila}"></input></td>' +
      '<td><button type="button" id="agregarArt" class="btn btn-primary"
            onclick="agregarDetalle()"><span class="fa fa-plus"></span></button><td>' +
      '<td><input type="hidden" id="idseriales${row.fila}" 
            value="idseriales${row.fila}"></input></td>'+
    '</tr>';
    $('#detalles').append(newFila);
};

//COMENTE TU CODIGO PARA HACER UNA PRUEBA

/*var cantidad = document.getElementsByName("cant")[0].value;
function agregarDetalle(idseriales,serial){
for (var i = 0; i <=cantidad; i++) {
var fila='<tr class="filas" id="fila'+cont+'">'+
        '<td><button type="button" class="btn btn-danger" onclick="eliminarDetalle('+cont+')">X</button></td>'+
        '<td><input type="text" name="serial[]" value="" id="serial[]"></input></td>'+
        '<td><button  type="button" id="agregarArt" class="btn btn-primary" onclick="agregarDetalle()"><span class="fa fa-plus"></span></button><td>'+
        '<td><input type="hidden" name="idseriales[]" id="idseriales[]" value="'+idseriales+'"></input></td>'+

        '</tr>';

        cont++;
        detalles=detalles+1;
        $('#detalles').append(fila); 
    }
}*/
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="cant">
<input type="button" id="add" value="Agregar Filas">
<table id="detalles"></table>
    
answered by 27.08.2018 в 19:26