How to add increment to each ID and reset after a new <div> PHP

Asked

Viewed 126 times

2

I want to know how many lines I have in each and which line will be modified, for that I want to add increment in id="id_tec_pag'+id_tec_pag+'[]". And that with each block change this counter will restart.

Body:

            html += '                           <tbody>';               
            html += '                               <tr class="linhas'+cdRegiao+'">';
            html += '                                   <input type="hidden" id="id_tec_pag'+id_tec_pag+'[]"/>';
            html += '                                   <td class="col-md-3"><input type="text" name="nome'+cdRegiao+'_tec[]"/></td>';
            html += '                                   <td class="col-md-3"><input type="text" name="email'+cdRegiao+'_tec[]"/></td>';
            html += '                                   <td class="col-md-1"><input type="text" name="telefone'+cdRegiao+'_tec[]"/></td>';
            html += '                                   <td class="col-md-2"><input type="password" name="senha1'+cdRegiao+'_tec[]"/></td>';
            html += '                                   <td class="col-md-2"><input type="password" name="senha2'+cdRegiao+'_tec[]"></td>';
            html += '                                   <td class="col-md-1"><button type="button" class="removerCampo"><i class="fa fa-close fa-lg"></i>&nbsp;&nbsp;<?=TXT_BOTAO_EXCLUIR?></button></td>';
            html += '                               </tr>';
            html += '                           </tbody>';
            html += '                       </table>';
            html += '                   </div>';
            html += '                   </div>';

            $('#div-regioes').append(html);

            adicionaCampo(cdRegiao);

JS:

    function adicionaCampo(codigo) {
        //$(".adicionarCampo").unbind("click");
        $(".adicionarCampo").on("click", function () {
            novoCampo = $("tr.linhas"+codigo+":first").clone();
            novoCampo.find("input").val("");
            novoCampo.insertAfter("tr.linhas"+codigo+":last");
            removeCampo(codigo);
        });
    }
  • You want the index (line number) or id on the line?

  • Try to change <input type="hidden" id="id_tec_pag'+id_tec_pag+'[]"/>'; for <td style='display: none' class="id">id_tec_pag</d>. Ai Voce takes this id in jquery with Closest. var row = $(this).closest('tr'); var id = row.find('.id').text()

  • Any way would help me, for example, I have a button that adds a div, and it starts with a region id =1, I want to know how many id_tec_pag the user added, when he adds another region, in the case of region = 2, I want to know how many id_tec_pag you have in region 2 and so on.

  • How you add this item to the table?

  • I was adding only Rows, but without differentiating which is which, so I can’t update the information when the user does editing.

  • adds a data-id in the field and ai recovers the value of the data-id and sum to obtain the n°of the new id.

Show 1 more comment

1 answer

0

To recover the amount of rows from your table you can use

var numeroLinhas = document.getElementById('seuId').rows.length;

If you want to go through each element of your list jquery makes it easy:

$('#suaTabela > tbody  > tr').each(function() { //Sua regra de negócio });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.