Validate checkbox from positive to negative in table

0

I want to place 0 or 1 if the checkbox of the row is selected, it works fine when passing the 1 but that is when it is checked, otherwise it does not work to pass it zero, not putting $(fila).closest('tr').find('.inputvalor').val("0"); with else.

var verificarTotal = function () {
        var $seleccionados = $(".chkseleccion:checked"),
            $cajas = $("#totalc"),
            $kilogramos = $("#totalp"),
            $t1 = $("#total1"),
            $t2 = $("#total2"),
            totalCajas = 0,
            totalKilogramos = 0;

        $.each($seleccionados, function (indice, fila) {
            if ($(fila).is(':checked')) {
                totalCajas += parseInt($(fila).closest('tr').find('.inputcajas').html());
                totalKilogramos += parseInt($(fila).closest('tr').find('.inputpeso').html());
                $(fila).closest('tr').find('.inputvalor').val("1");
            } else {
           $(fila).closest('tr').find('.inputvalor').val("0");
        }
        });


        $cajas.val(totalCajas);
        $kilogramos.val(totalKilogramos);
        $t1.text(totalCajas);
        $t2.text(totalKilogramos);

    };

Does anyone know what's going on?

my table:

<table id="datatable-buttons" class="table table-striped table-bordered dataTable no-footer dtr-inline" role="grid" aria-describedby="datatable-buttons_info" style="width: 100%;">
                              <thead>
                                <tr role="row">
                                    <th class="sorting_asc" tabindex="0" aria-controls="datatable-buttons" rowspan="1" colspan="1" style="width: 20px;" aria-sort="ascending" aria-label="">#</th>
                                    <th class="sorting" tabindex="0" aria-controls="datatable-buttons" rowspan="1" colspan="1" style="width: 50px;" aria-label="">Peso de tarima</th>
                                    <th class="sorting" tabindex="0" aria-controls="datatable-buttons" rowspan="1" colspan="1" style="width: 50px;" aria-label="">Cajas de tarima</th>
                                    <th class="sorting" tabindex="0" aria-controls="datatable-buttons" rowspan="1" colspan="1" style="width: 50px;" aria-label="">Enviar</th>
                              </thead>

                                <tbody>
                                    <?php if(!empty($tarimas)): ?>
                                        <?php foreach($tarimas as $tarimas): ?>
                                        <tr role="row" class="odd">
                                            <td tabindex="0" class="sorting_1">
                                                <input type="number" value="0" name='valores[]' class="inputvalor">
                                                <input type="hidden" value="<?php echo $tarimas->id;?>" name='inputseleccion[]'><?php echo $tarimas->id; ?>
                                            </td>
                                            <td class="inputpeso"><?php echo $tarimas->peso; ?> kgs</td>
                                            <td class="inputcajas"><?php echo $tarimas->cajas; ?> cajas</td>
                                            <td>
                                                <div class="checkbox">
                                                <label>
                                                  <input type="checkbox" value="" name='inputselect[]' class="chkseleccion"> Añadir
                                                </label>
                                                </div>
                                          </td>
                                        </tr>
                                        <?php endforeach; ?>
                                    <?php endif; ?>
                                </tbody>
                        </table>
    
asked by DoubleM 24.04.2018 в 01:10
source

1 answer

2

Your problem is in this line:

var $seleccionados = $(".chkseleccion:checked"),

You are selecting only the values that are checked for the class. Just remove the last part like this:

var $seleccionados = $(".chkseleccion"),
    
answered by 24.04.2018 / 01:24
source