How does Alert show when marking checkbox in a table?

Asked

Viewed 577 times

3

I have a table:

The information shown is searched in the bank, and on the column side (in this example) - Salary - you will have a column called Lock / Unlock - where I can block that information in the bank (changing the value from 0 to 1) and so before the name - example Ashton Cox will have a green icon that means unlocked and a red one if it is locked.

By marking the checkbox, I wanted an Alert to appear "Are you sure you want to block?" and the same thing to unlock "Are you sure you want to unlock?"

Like I’m doing:

<tbody>
  <?php

      while($linhaAssociativa = mysqli_fetch_assoc($query))
      { ?>
        <tr>
          <td><?php echo $linhaAssociativa["Name"]; ?></td>
          <td><?php echo $linhaAssociativa["Position"]?></td>
          <td><?php echo $linhaAssociativa["Office"]?></td>
          <td><?php echo $linhaAssociativa["Age"]?></td>
          <td><?php echo $linhaAssociativa["Start date"]?></td>
          <td><?php echo $linhaAssociativa["Salary"]?></td>
          <td><input type="checkbox" name="muda" id="muda" /></td>
        </tr>
    <?php
      } ?>

</tbody>

I’m not able to do javascript/jquery code for that: when checking a checkbox of such a row of the table show if it is not locked: "Are you sure you want to block?" or show if locked: "Are you sure you want to unlock?" - on an Alert and change the value from 0 to 1 or 1 to 0

  • 1

    Create an expensive JS file to do this, so you will be able to capture the action of clicking on the checkbox and change from locked to unlocked and vice versa .

  • Have you ever done any script ?

  • I edited my question and put in bold what my real question is. @Dotnet I haven’t made any script yet. What I’m afraid will happen: as checkboxes are being generated inside that while, they won’t generate with the same id/name? If yes, when dialing to block, it would not block all lines?

  • Inside each new checkbox? And the script to "block/unlock" and show an Alert before... How would it look? Or better said.. How I would script to select certain checkbox?

2 answers

6

Do something like that:

In HTML:

<input name="muda" type="checkbox" onchange="mudar(this);">

In Javascript:

function mudar(obj){
    var selecionado = obj.checked;
    if (selecionado) {
        alert('Tem certeza de quer quer bloquear?');
    } else {
        alert('Tem certeza de que quer desbloquear?');
    }
}
  • I added, but when I click on the checkbox nothing happens...

  • @Cesara.Fonseca Some error in the browser console?

2

Follow the example. This way it will work for all checkboxes

$(document).ready(function() {
  $("input[type='checkbox']").on('click', function() {
    if (!$(this).prop('checked')) {
      alert('Tem certeza de que quer desbloquear?');
      return true;
    }
    alert('Tem certeza de quer quer bloquear?');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />

Browser other questions tagged

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