capture value of an array checkbox in a table

-1

I have the following code:

var fila='<tr class="selected" id="fila'+cont+'" onclick="seleccionar(this.id);"><td>'+cont+'</td><td>'+$('input[name="fk_animalito"]').val();+'</td><td>'+$($('input[name^="fk_hora_sorteo"]')[0]).val()+'</td><td>'+$('input[name="monto"]').val();+'</td></tr>';

The input type="text" captures them well and they show me in the table perfectly, the problem is: input type='checkbox' name='fk_hora_sorteo[]' that as I am passing it as array I do not capture the value to show it in the table; I have tried like this, but it does not work for me:

$($('input[name^="fk_hora_sorteo"]')[0]).val()

The form:

<form id="idFormulario" action="" method="POST">

  <!-- section -->
  <section>
    <div class="row">
      <div class="container">
        <?php
        $consulta = $DB_con->query("SELECT * FROM hora_sorteo ORDER BY id");
        if($consulta->rowCount() > 0) {

          while ($linea = $consulta->fetch(PDO::FETCH_ASSOC)) {
            echo "<div class='col s6 m2'>";
            echo "<input type='checkbox' name='fk_hora_sorteo[]' id='chk1{$linea['id']}' value='{$linea['id']}'>";
            echo "<label class='black-text' for='chk1{$linea['id']}'>{$linea['hora_sorteo']}</label>";
            echo "</div>";
          }
        }
        else
          echo "<div class='col s12 card-panel yellow darken-2 center'>
          <h5 class='black-text text-darken-2 center CONDENSED LIGHT5'>¡ 
          Advertencia: No se ha encontrado ningún registro !</h5>
          </div>";
        ?>
      </div>
    </div>
  </section>
  <section>
    <div class="row">
      <?php
      $consulta = $DB_con->query("SELECT * FROM animalitos ORDER BY id");
      if($consulta->rowCount() > 0) {
        $i=1;
        while ($linea = $consulta->fetch(PDO::FETCH_ASSOC)) {
      ?>
          <div class="col s1 m1">
            <div class="chip">
              <img src="../galerias_animalitos/<?= $linea['portada']?>"  alt="imagen animalitos"/>
              <?php echo $linea['numero']; ?> <?php echo $linea['nombre']; ?>
            </div>
          </div>
      <?php
      $i++;
        }  
      }
      else
        echo "<div class='col s12 card-panel yellow darken-2 center'>
        <h5 class='black-text text-darken-2 center CONDENSED LIGHT5'>¡ 
        Advertencia: No se ha encontrado ningún registro !</h5>
        </div>";
      ?>
    </div>
  </section>
  <!-- fin section -->

  <div class="row">
    <div class="col s12 m5">
     <div class="input-field col s12 m4">
       <input type="text" name="fk_animalito" autocomplete="off" required/>
       <label for="descripcion" class="black-text ">Numero Animalito:</label>
     </div>
    <div class="input-field col s12 m4">
      <input id="bt_add" type="text" name="monto" autocomplete="off" required/>
      <label for="descripcion" class="black-text ">Monto:</label>
    </div>

    <br>
    <br>
    <br>
    <br>

    <button class="btn waves-effect blue darken-4 btn-small" type="submit" name="guardar">Generar Jugada</button>
    <button id="bt_del" class="btn waves-effect red btn-medium">Eliminar</button>
    <button id="bt_delall" class="btn waves-effect red btn-medium">Eliminar Todo</button>

    <div class="col center s12 m12">
      <h4 class="left-align black-text">Serial:             Total:</h4>
   </div>
  </div>
</form>

<div class="col s12 m7">
  <div style=' overflow: auto; height: 250px;'>
    <table id="tabla_ventas" class='bordered responsive-table centered'>
      <thead>
        <tr>
          <th data-field='N&#186;'>N°</th>
          <th data-field='N&#186;'>ANIMALITO</th>
          <th data-field='Código'>SORTEO</th>
          <th data-field='Código'>MONTO</th>
        </tr>
      </thead>
    </table>
  </div>
</div>

Annex the image:

    
asked by yoclens 27.05.2017 в 20:01
source

2 answers

0

Those ";" they were spoiling the exit, your code should look like this:

var fila='<tr class="selected" id="fila'+cont+'" onclick="seleccionar(this.id)"><td>'+cont+'</td><td>'+$('input[name="fk_animalito"]').val()+'</td><td>'+$('input[name="fk_hora_sorteo"]').val()+'</td><td>'+$('input[name="monto"]').val()+'</td></tr>';

Try this example, they are inputs with the names you are referring to in your row variable, if you enter values, you will see that the tr is created correctly:

<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>Hi</title>

<!-- JQuery  -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<script>
$(document).ready(Principal);
function Principal(){
  $("button").click(Pone);
}
function Pone(){
  var cont = 0;
  var fila='<tr class="selected" id="fila'+cont+'" onclick="seleccionar(this.id);"><td>'+cont+'</td><td>'+$('input[name="fk_animalito"]').val()+'</td><td>'+$('input[name="fk_hora_sorteo"]').val()+'</td><td>'+$('input[name="monto"]').val()+'</td></tr>';
  $("body").append(fila);
}

</script>
</head>
<body>
  <input type="text" name="fk_animalito">
  <input type="text" name="fk_hora_sorteo">
  <input type="text" name="monto">
  <button>Aprétame</button>
</body>
</html>

I hope I have helped you.

    
answered by 27.05.2017 в 20:22
-1

the solution to the problem

function agregar() {
var fila = '';
$('input[name^="fk_hora_sorteo"]').filter(':checked').each(function() {
    cont++;
    fila = '<tr class="selected" id="fila' + cont + '" onclick="seleccionar(this.id)"><td>' + cont + '</td><td>' + $('input[name="fk_animalito"]').val() + '</td><td>' + $(this).val() + '</td><td>' + $('input[name="monto"]').val() + '</td></tr>';
    $('#tabla_ventas').append(fila);
    reordenar();
})
}
    
answered by 28.05.2017 в 01:18