1

Supogamos que tengo una tabla como la siguiente:

<table id="parameters" width="100%" border="1">
  <thead>
    <tr>
      <th>Fecha</th>
      <th>Cedula</th>
      <th>Nombre Empleado</th>
      <th>Cantidad</th>
      <th>Concepto</th>
      <th>Seleccionar</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>2016-08-10<input type="hidden" name="fecha[]" value="2016-08-10"></td>
      <td>12345678<input type="hidden" name="identity[]" value="12345678"></td>
      <td>Pepito Perez</td>
      <td>2.5<input type="hidden" name="value[]" value="2.5"></td>
      <td>Hora diurna<input type="hidden" name="concept[]" value="1"></td>
      <td><input type="checkbox" name="check[]" value="12345678"></td>
    </tr>
    <tr>
      <td>2016-08-10<input type="hidden" name="fecha[]" value="2016-08-10"></td>
      <td>24681012<input type="hidden" name="identity[]" value="24681012"></td>
      <td>Camilo Sanchez</td>
      <td>2.5<input type="hidden" name="value[]" value="2.5"></td>
      <td>Hora diurna<input type="hidden" name="concept[]" value="1"></td>
      <td><input type="checkbox" name="check[]" value="24681012"></td>
    </tr>
    <tr>
      <td>2016-08-10<input type="hidden" name="fecha[]" value="2016-08-10"></td>
      <td>369121518<input type="hidden" name="identity[]" value="369121518"></td>
      <td>Pepito Perez</td>
      <td>2.5<input type="hidden" name="value[]" value="2.5"></td>
      <td>Hora diurna<input type="hidden" name="concept[]" value="1"></td>
      <td><input type="checkbox" name="check" value="369121518"></td>
    </tr>
  </tbody>
  </table>

Esta tabla se genera de acuerdo a una consulta, el id del checkbox es el valor identity de cada empleado.

Quiero obtener el valor de los input hidden solamente de los checkbox marcados en un arreglo para luego enviar esos datos por ajax y luego guardarlos en la base de datos, use lo siguiente para guardar los datos en un objeto pero no lo guarda bien:

//Aquí al dar click en el boton confirmar busca en la tabla los que estan marcados
confirm.on('click', function(){
//Guardo en result los inputs con el checkbox marcado
      $("#parameters tbody tr td input[name=checks]:checked").each(function(){
        var result = [];
        $(this).closest('tr').find('input[type="hidden"]').each(function(){
          result.push($(this).serializeArray());
        });

//envio el objeto por ajax
        $.ajax({
          type: "POST",
          url:  getBaseUri()+'/payroll/createAll/',
          data: {datas: result},
          cache: false,
          success: function (response) {
            var table = tableConfirm;
            var url = table.data('source');
            clearFom();
            table.dataTable().fnReloadAjax(url);
          },
          error: function (response) {
            console.log(response);
          }
        });
      });

Quisiera saber que estoy haciendo mal y si hay una mejor manera de enviar los datos por ajax para poder guardarlos en la base de datos

1 respuesta 1

1

Hay un problema en el selector de los checkboxes:

$("#parameters tbody tr td input[name=checks]:checked")

Si te fijas, estás seleccionando los inputs cuyo nombre sea "checks", pero no hay ninguno que tenga ese nombre. Los checkboxes tienen nombre "check[]". Una solución rápida sería cambiar ese selector para que escoja los input de tipo "checkbox" (que sólo hay uno por fila):

$("#parameters tbody tr td input[type=checkbox]:checked")

Cambiando eso, ya funciona sin problemas:

//Aquí al dar click en el boton confirmar busca en la tabla los que estan marcados
$("#confirm").on('click', function(){

  //Guardo en result los inputs con el checkbox marcado
  $("#parameters tbody tr td input[type=checkbox]:checked").each(function(){
    var result = [];
    $(this).closest('tr').find('input[type="hidden"]').each(function(){
      result.push($(this).serializeArray());
    });

    console.log(result);

    //envio el objeto por ajax

  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<form>
  <table id="parameters" width="100%" border="1">
    <thead>
      <tr>
        <th>Fecha</th>
        <th>Cedula</th>
        <th>Nombre Empleado</th>
        <th>Cantidad</th>
        <th>Concepto</th>
        <th>Seleccionar</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>2016-08-10<input type="hidden" name="fecha[]" value="2016-08-10"></td>
        <td>12345678<input type="hidden" name="identity[]" value="12345678"></td>
        <td>Pepito Perez</td>
        <td>2.5<input type="hidden" name="value[]" value="2.5"></td>
        <td>Hora diurna<input type="hidden" name="concept[]" value="1"></td>
        <td><input type="checkbox" name="check[]" value="12345678"></td>
      </tr>
      <tr>
        <td>2016-08-10<input type="hidden" name="fecha[]" value="2016-08-10"></td>
        <td>24681012<input type="hidden" name="identity[]" value="24681012"></td>
        <td>Camilo Sanchez</td>
        <td>2.5<input type="hidden" name="value[]" value="2.5"></td>
        <td>Hora diurna<input type="hidden" name="concept[]" value="1"></td>
        <td><input type="checkbox" name="check[]" value="24681012"></td>
      </tr>
      <tr>
        <td>2016-08-10<input type="hidden" name="fecha[]" value="2016-08-10"></td>
        <td>369121518<input type="hidden" name="identity[]" value="369121518"></td>
        <td>Pepito Perez</td>
        <td>2.5<input type="hidden" name="value[]" value="2.5"></td>
        <td>Hora diurna<input type="hidden" name="concept[]" value="1"></td>
        <td><input type="checkbox" name="check" value="369121518"></td>
      </tr>
    </tbody>
  </table>
  <button type="button" id="confirm">Confirmar</button>
</form>

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.