When selecting checkbox it selects other checkbox

Asked

Viewed 187 times

2

I am not aware of Javascript and I am using Cakephp 3 framework. So my problem is, I have a list where each item on the list has 2 checkbox, and when I select the first checkbox for one item it would automatically select the other.

What happens is that when I select the first checkbox of the item it selects the second checkbox of the entire list and not only the second checkbox of the selected item.

This is my view:

<form action="../vendas/" method="post">
<?= $this->Form->create("carrinho", ["class " => "form-add", "action" => "carrinho", "controller" => "Produtos-Vendas"]) ?>
<button type="submit" class="btn-success btn">Carrinho (<span id="additem"></span>)</button>
    <?php foreach ($produtos as $produto): ?>
        <tr>
            <td><?= $this->Number->format($produto->id) ?></td>
            <td><?= h($produto->name) ?></td>
            <td><?= h($produto->cor) ?></td>
            <td><?= h($produto->tecido) ?></td>
            <td><?= $this->Number->format($produto->estoque) ?></td>
            <td><?= $this->Number->format($produto->preco) ?></td>
            <td>
            <input type="checkbox" id="checkTodos" name="check[]" class="checkbox checkTodos" value="<?php echo $produto['id'];?>">
            <input type="checkbox" class="checkado" name="nome" value="<?php echo $produto['name'];?>">
            </td>
         </tr>   
         <?php endforeach; ?>
         <?= $this->Form->end() ?>
</form>

Javascript:

$(".checkTodos").click(function(){
    $('.checkado' && '#checkado').not(this).prop('checked', this.checked);
});
  • If uncheck the first is to uncheck the second tb?

2 answers

1

Notice that you are going through a list of products, and for each product you generate a row in a table. But your id checkbox is the same for all rows in that table. Try to generate an id for each row, maybe using some attribute from your product. tries to change and tells us the result.
EDIT: Try something like this:

<?php foreach ($produtos as $produto): ?>
    <tr>
        <td><?= $this->Number->format($produto->id) ?></td>
        <td><?= h($produto->name) ?></td>
        <td><?= h($produto->cor) ?></td>
        <td><?= h($produto->tecido) ?></td>
        <td><?= $this->Number->format($produto->estoque) ?></td>
        <td><?= $this->Number->format($produto->preco) ?></td>
        <td>
        <input type="checkbox" id="<?=$produto->id;?>" name="check[]" class="checkbox checkTodos" value="<?php echo $produto['id'];?>">
        <input type="checkbox" class="checkado" name="nome" value="<?php echo $produto['name'];?>">
        </td>
     </tr>   
     <?php endforeach; ?>

1


Your selector is wrong. You should only take care of the checkbox checked ($(this)) and not all.

Use .next() to select the checkbox adjacent to the checkbox checked:

$(".checkTodos").click(function(){
   $(this).next().prop('checked', this.checked);
});

When doing the loop, you are repeating the id="checkTodos", what is wrong, for a id cannot be repeated on the page. Or you use class="checkTodos" or remove if not useful.

  • Thank you that was the best solution to my problem!

  • Thanks to everyone who helped me, I tried to solve with idea to put in the checkbox: id="id;? > But it didn’t work at first, I thought I’d see another way to generate different ID for each checkbox, but before I tested the suggestion of . next() and it worked successfully! Thank you so much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.