Get values row table

4

I need to get the values for each row in my table. My table has no fixed number of rows and columns.

I only want the values of inputs (within td ). The desired inputs have class quantidadeDigitada

The ideal would be to create an array with values per line. For example:

Valores[0] = 20.40;
Valores[1] = 20.40;
Valores[2] = 20.40;

I need to get the data as follows:

Valores.toString() = 20.40,20.40,20.40;

With this I can identify the quantity per product based on the position of array

    
asked by anonymous 17.03.2015 / 18:50

1 answer

5
  • First select your inputs :

    $('.quantidadeDigitada')
    
  • Then get your lines:

    .closest("tr")
    
  • Then map each line to a value:

    .map(function() {
    
  • Return the value of% s with% s formatted as either:

        return $(this).find("input:eq(0)").val() + "." +
               $(this).find("input:eq(1)").val();
    })
    
  • The result will be an array where each element is a row. Finally, merge them with the comma:

    .toArray()
    .join(',')
    
  • Complete example:

    alert(
        $('.quantidadeDigitada')
            .closest("tr")
            .map(function() {
                return $(this).find("input:eq(0)").val() + "." +
                       $(this).find("input:eq(1)").val();
            })
            .toArray()
            .join(',')
    );
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><table><tr><td><inputclass="quantidadeDigitada" value="10"></td>
        <td><input class="quantidadeDigitada" value="20"></td>
      </tr>
      <tr>
        <td><input class="quantidadeDigitada" value="30"></td>
        <td><input class="quantidadeDigitada" value="40"></td>
      </tr>
      <tr>
        <td><input class="quantidadeDigitada" value="50"></td>
        <td><input class="quantidadeDigitada" value="60"></td>
      </tr>
    </table>
        
    17.03.2015 / 19:08