How does alert show when checking checkbox in a table?

3

I have a table:

Theinformationshownissearchedinthebank,andonthesideofthecolumn(inthisexample)-Salary-willhaveacolumncalledLock/Unlock-whereIcanlockthatinformationinthebankfrom0to1)andsobeforethename-exampleAshtonCoxwillhaveagreeniconthatmeansunlockedandaredifitislocked.

Whencheckingthecheckbox,IwantedtoseeanalertAreyousureyouwanttoblock?andthesamethingtounlock"Are you sure What do you want to unlock? "

How 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 the javascript / jquery code so that: Checking the checkbox of a row of the table shows if it is not blocked: "Are you sure you want to block?" or show if it's locked: "Are you sure you want to unlock?" - in an alert and change the value from 0 to 1 or 1 to 0

    
asked by anonymous 14.06.2016 / 17:21

2 answers

5

Do something like this:

No 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?');
    }
}
    
14.06.2016 / 19:08
2

Follow the example. That 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><inputtype="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
    
16.06.2016 / 16:02