In a row created by Jquery, how to name inputs that are created?

0

Good morning. I have the following situation, I have a table, which generates rows every time the 'add' button is pressed, within the created TDs there are some input that keep the information stored, all the above is inside of a form that goes to a file that inserts the database, but how can I name input dynamically by jquery ?

My code is this:

function addProduct(e) {
  e.preventDefault();
  const row = createRow({
    codP: $('#codP').val(),
    qty: $('#qty').val(),
	nomP: $('#nomP').val(),
	typeP: $('#typeP').val(),
	presentationP: $('#presentationP').val(),
	priceP: $('#priceP').val(),
	total: $('#total').val()
  });
  $('#tabla tbody').append(row);
  clean();
}

function createRow(data) {
  return (
    '<tr align='center' id='fila'>' +
      '<td><input type='text' name='resulta' value='${data.codP}' disabled='disabled'></td>' +
      '<td><input type='text' name='resultb' value='${data.qty}' size='1' disabled='disabled'></td>' +
	  '<td><input type='text' name='resultc' value='${data.nomP}' disabled='disabled'></td>' +
	  '<td><input type='text' name='resultd' value='${data.typeP}' size='10' disabled='disabled'></td>' +
	  '<td><input type='text' name='resulte' value='${data.presentationP}' size='10' disabled='disabled'></td>' +
	  '<td><b>¢</b><input type='text' name='resultf' value='${data.priceP}' size='10' disabled='disabled'></td>' +
	  '<td><b>¢</b><input type='text' name='resultg' value='${data.total}' size='10' disabled='disabled'></td>' +
	  '<td><input id='borrarFila' type='button' value='Eliminar' title='Eliminar Línea'></td>' +
    '</tr>'
  );
}


function clean() {
  $('#codP').val('');
  $('#qty').val('');
  $('#nomP').val('');
  $('#typeP').val('');
  $('#presentationP').val('');
  $('#priceP').val('');
  $('#total').val('');
  $('#codP').focus();
}
<table width="100%" align="center" bordercolor="#000000" border="1" id="tabla">
    	<tr align="left">
        	<td colspan="7">
            <h2 align="left">&nbsp;&nbsp;IV. Orden de Pedido</h2>
            </td>
            <td colspan="2">
            Usuario que registra: <input type="text" size="7" name="creador" value="<?php echo $_SESSION['idu'] ?>" disabled="disabled" />
            </td>
        </tr>
    	<tr align="center">
        	<td valign="top">Código/UPC</td>
            <td valign="top">Cantidad</td>
            <td valign="top">Producto</td>
            <td valign="top">Tipo de Pan</td>
            <td valign="top">Presentación</td>
            <td valign="top">Precio</td>
            <td valign="top">Total</td>
            <td valign="top" colspan="2">Administrar</td>
        </tr>
        <div class="ui-widget">
        <tr align="center" id="esta"><div class="ui-widget">
        	<td valign="top"><input type="text" id="codP" class="codP" name="ean" placeholder="Código del Producto" maxlength="13" autocomplete="off" title="Ingrese el Código del Producto" />
            
            </td>
            			<td valign="top">
						<select name="qty" id="qty" title="Seleccione la Cantidad" onChange="multiplicar();">
            	<option value="">0</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
                <option value="9">9</option>
                <option value="10">10</option>
            			</select>	
                        </td>
            <td valign="top">
            					
							<input type="text" id="nomP" name="PName" placeholder="Nombre del Producto" disabled="disabled" />
						</td>
            <td valign="top"><input type="text" name="typeP" id="typeP" size="10" title="Tipo de Producto"  disabled="disabled" /></td>
            <td valign="top"><input type="text" name="presentationP" id="presentationP" size="10" title="Presentación del Producto"  disabled="disabled" /></td>
            <td valign="top"><b>¢</b><input type="text" name="priceP" id="priceP" size="10" title="Precio Unitario"  disabled="disabled" /></td>
            <td valign="top"><b>¢</b><input type="text" name="total" id="total" size="10" title="Total de Fila"  disabled="disabled" /></td>
            <td valign="top"><input id="agregarFila" type="button" value="Agregar" onclick="addProduct(event)" title="Agregar Producto"/></td>
            </div>
        </tr>
    </table>

Then, in concert, I need to place the name value to these input that are generated with this code

function createRow(data) {
  return (
    '<tr align='center' id='fila'>' +
    '<td><input type='text' name='resulta' value='${data.codP}' 
      disabled='disabled'></td>' +

As you see the name that I put in 'it turns out' it would need to be generated as 'results1' , 'results2' , 'results3' , etc.

Thanks for helping.

    
asked by Diego_Esquivel 21.12.2017 в 16:47
source

1 answer

0

To have the dynamic names you can add this name='resulta${$('#tabla tbody').find("tr").length} that will return a unique number for each tr and children input I hope you help greetings.

  

NOTE: Obtain the total of tr in the table with $('#tabla tbody').find("tr").length and that number is the one assigned to the name which means that it is unique for each input within it.

function addProduct(e) {
  e.preventDefault();
  const row = createRow({
    codP: $('#codP').val(),
    qty: $('#qty').val(),
	nomP: $('#nomP').val(),
	typeP: $('#typeP').val(),
	presentationP: $('#presentationP').val(),
	priceP: $('#priceP').val(),
	total: $('#total').val()
  });
  $('#tabla tbody').append(row);
  clean();
}

function createRow(data) {
  return (
    '<tr align='center' id='fila'>' +
      '<td><input type='text' name='resulta${$('#tabla tbody').find("tr").length}' value='${data.codP}' disabled='disabled'></td>' +
      '<td><input type='text' name='resultb${$('#tabla tbody').find("tr").length}' value='${data.qty}' size='1' disabled='disabled'></td>' +
	  '<td><input type='text' name='resultc${$('#tabla tbody').find("tr").length}' value='${data.nomP}' disabled='disabled'></td>' +
	  '<td><input type='text' name='resultd${$('#tabla tbody').find("tr").length}' value='${data.typeP}' size='10' disabled='disabled'></td>' +
	  '<td><input type='text' name='resulte${$('#tabla tbody').find("tr").length}' value='${data.presentationP}' size='10' disabled='disabled'></td>' +
	  '<td><b>¢</b><input type='text' name='resultf' value='${data.priceP}' size='10' disabled='disabled'></td>' +
	  '<td><b>¢</b><input type='text' name='resultg${$('#tabla tbody').find("tr").length}' value='${data.total}' size='10' disabled='disabled'></td>' +
	  '<td><input id='borrarFila' type='button' value='Eliminar' title='Eliminar Línea'></td>' +
    '</tr>'
  );
}


function clean() {
  $('#codP').val('');
  $('#qty').val('');
  $('#nomP').val('');
  $('#typeP').val('');
  $('#presentationP').val('');
  $('#priceP').val('');
  $('#total').val('');
  $('#codP').focus();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table width="100%" align="center" bordercolor="#000000" border="1" id="tabla">
    	<tr align="left">
        	<td colspan="7">
            <h2 align="left">&nbsp;&nbsp;IV. Orden de Pedido</h2>
            </td>
            <td colspan="2">
            Usuario que registra: <input type="text" size="7" name="creador" value="<?php echo $_SESSION['idu'] ?>" disabled="disabled" />
            </td>
        </tr>
    	<tr align="center">
        	<td valign="top">Código/UPC</td>
            <td valign="top">Cantidad</td>
            <td valign="top">Producto</td>
            <td valign="top">Tipo de Pan</td>
            <td valign="top">Presentación</td>
            <td valign="top">Precio</td>
            <td valign="top">Total</td>
            <td valign="top" colspan="2">Administrar</td>
        </tr>
        <div class="ui-widget">
        <tr align="center" id="esta"><div class="ui-widget">
        	<td valign="top"><input type="text" id="codP" class="codP" name="ean" placeholder="Código del Producto" maxlength="13" autocomplete="off" title="Ingrese el Código del Producto" />
            
            </td>
            			<td valign="top">
						<select name="qty" id="qty" title="Seleccione la Cantidad" onChange="multiplicar();">
            	<option value="">0</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
                <option value="9">9</option>
                <option value="10">10</option>
            			</select>	
                        </td>
            <td valign="top">
            					
							<input type="text" id="nomP" name="PName" placeholder="Nombre del Producto" disabled="disabled" />
						</td>
            <td valign="top"><input type="text" name="typeP" id="typeP" size="10" title="Tipo de Producto"  disabled="disabled" /></td>
            <td valign="top"><input type="text" name="presentationP" id="presentationP" size="10" title="Presentación del Producto"  disabled="disabled" /></td>
            <td valign="top"><b>¢</b><input type="text" name="priceP" id="priceP" size="10" title="Precio Unitario"  disabled="disabled" /></td>
            <td valign="top"><b>¢</b><input type="text" name="total" id="total" size="10" title="Total de Fila"  disabled="disabled" /></td>
            <td valign="top"><input id="agregarFila" type="button" value="Agregar" onclick="addProduct(event)" title="Agregar Producto"/></td>
            </div>
        </tr>
    </table>
    
answered by 21.12.2017 в 17:20