How to select a tr element in the middle of "N tr's" in jQuery?

2

It has a table mounted, that each row is added dynamically. Therefore, within <tbody> is added the <tr's> (lines).

Inside the tr, it has the <td's> and each <td> has a input inside.

Imagine that I can added 5 t's. So if I want to get a value of the first tr , I use:

$('#d_sfiSRV003 #d_GRD_CENTROS_CUSTO tr:first-child').find('input#f_VLR_APROP_CCUST').val();

d_sfiSRV003 - id da pagina;
d_GRD_CENTROS_CUSTO - id da tabela
tr:first-child - seleciono o primeiro tr q tiver
input#f_VLR_APROP_CCUST - busco o input com a id referida e pego seu valor.

But if the tr's are dynamically placed, how do I get for example 6 tr's placed, get the value of input#f_VLR_APROP_CCUST of the 4th tr or whichever one I choose?

    
asked by anonymous 23.02.2018 / 19:53

3 answers

0

To get the elements without knowing your ids you can do this:

Paste 3˚ tr:

$('tr:nth-child(3)')

Paste tr with name:

$('tr[name="nome1"]'')

If you wanted to, you can still know how many three you have this way:

var count = $('#id_objeto tr').length

I hope this helps you!

    
23.02.2018 / 20:13
0

With a simple loop, you can work with all children of an html tag at a time:

$("tr").each(function(){
    ... código ...
})

In the loop you can work with objects the way you want them to

About the jquery's each function, it's pretty powerful

    
23.02.2018 / 20:38
0

Do not use ids to capture dynamically added elements (even for those that already exist). A id must be unique on the page.

Change the id f_VLR_APROP_CCUST to class : class="f_VLR_APROP_CCUST" .

Next you will be able to get the value of input with the selector:

$('#d_sfiSRV003 #d_GRD_CENTROS_CUSTO tr:nth-child(numero_da_linha)')
.find('.f_VLR_APROP_CCUST').val()

Where numero_da_linha is the line number you want to find input .

    
23.02.2018 / 20:52