Recorrer tabla bootstrap con jQuery text y value HTML5

publicado por: Anonymous

Tengo un problema en jQuery.
Estoy recorriendo una tabla de bootstrap con ciertos valores, necesito que los valores que se corren se guarden en un arreglo, en este momento estoy recorriendo los valores por separado, no sé como puedo hacer para recorrer todos los valores y guardarlos en un array.

Quisiera que quedara de la siguiente forma:

valores = [modelo, codigo, precio, piezasporcaja, cantidaddepiezas]

Esto es lo que tengo hasta ahora:

_x000D_

_x000D_

 $("#ok").click(function() {_x000D_
   info = ""_x000D_
   $(".valores").parent("tr").find("td").each(function() {_x000D_
     info += $(this).text() + ",";_x000D_
     // info += this.value+ " ";_x000D_
   });_x000D_
_x000D_
   $('.valores').each(function(e) {_x000D_
     info += this.value + " ";_x000D_
   });_x000D_
   console.log(info);_x000D_
   alert(info);_x000D_
 })

_x000D_

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>_x000D_
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<table class="table">_x000D_
  <thead class="thead-light">_x000D_
    <tr>_x000D_
      <th scope="col">Modelo</th>_x000D_
      <th scope="col">Codigo</th>_x000D_
      <th scope="col">Precio</th>_x000D_
      <th scope="col">Piezas por caja</th>_x000D_
      <th scope="col">Cantidad de Piezas</th>_x000D_
    </tr>_x000D_
  </thead>_x000D_
  <tbody>_x000D_
    <tr>_x000D_
      <th class='valores' scope="row">SILENT 100 DESIGN</th>_x000D_
      <td>5DESIGN100</td>_x000D_
      <td>$ 62.00 USD</td>_x000D_
      <td>4</td>_x000D_
      <td> <input type='number' class='form-control valores' name='qtyMulti' value='0' required min='0' max='99999'> </td>_x000D_
    </tr>_x000D_
    <tr>_x000D_
      <th class='valores' scope="row">SILENT 200 DESIGN</th>_x000D_
      <td>5DESIGN200</td>_x000D_
      <td>$ 85.00 USD</td>_x000D_
      <td>4</td>_x000D_
      <td> <input type='number' class='form-control valores' name='qtyMulti' value='0' required min='0' max='99999'> </td>_x000D_
    </tr>  _x000D_
  </tbody>_x000D_
</table>_x000D_
_x000D_
<input type="button" value="ok" id="ok" class="boton2">

_x000D_

_x000D_

_x000D_

solución

Puedes hacerlo dandole la clase valores a las lineas en lugar de a los th asi:

_x000D_

_x000D_

$("#ok").click(function() {_x000D_
  var info=[];_x000D_
  $(".table").find("tr.valores").each(function() {_x000D_
               var row = [];_x000D_
               var flag = false;_x000D_
               $(this).children().each(function() {_x000D_
                   var input = $(this).find('input');_x000D_
                   if (input[0]) {_x000D_
                     if (input.val() > 0) {_x000D_
                       flag = true;_x000D_
                       row.push(input.val());_x000D_
                     }_x000D_
                   } else {_x000D_
            	     row.push($(this).text() );_x000D_
                   }_x000D_
       	       });             _x000D_
               if (flag) {_x000D_
                 info.push(row);_x000D_
               }_x000D_
  });_x000D_
  console.log(info);_x000D_
  alert(info);_x000D_
})

_x000D_

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>_x000D_
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<table class="table">_x000D_
  <thead class="thead-light">_x000D_
    <tr>_x000D_
      <th scope="col">Modelo</th>_x000D_
      <th scope="col">Codigo</th>_x000D_
      <th scope="col">Precio</th>_x000D_
      <th scope="col">Piezas por caja</th>_x000D_
      <th scope="col">Cantidad de Piezas</th>_x000D_
    </tr>_x000D_
  </thead>_x000D_
  <tbody>_x000D_
    <tr class='valores'>_x000D_
      <th scope="row">SILENT 100 DESIGN</th>_x000D_
      <td>5DESIGN100</td>_x000D_
      <td>$ 62.00 USD</td>_x000D_
      <td>4</td>_x000D_
      <td> <input type='number' class='form-control valores' name='qtyMulti' value='0' required min='0' max='99999'> </td>_x000D_
    </tr>_x000D_
    <tr class='valores'>_x000D_
      <th scope="row">SILENT 200 DESIGN</th>_x000D_
      <td>5DESIGN200</td>_x000D_
      <td>$ 85.00 USD</td>_x000D_
      <td>4</td>_x000D_
      <td> <input type='number' class='form-control valores' name='qtyMulti' value='0' required min='0' max='99999'> </td>_x000D_
    </tr>  _x000D_
  </tbody>_x000D_
</table>_x000D_
_x000D_
<input type="button" value="ok" id="ok" class="boton2">

_x000D_

_x000D_

_x000D_

Respondido por: Anonymous

Leave a Reply

Your email address will not be published. Required fields are marked *