vendredi 11 septembre 2015

Uncheck checkbox if checked with jquery

I have 3 checkboxes in a row and this code I found works GREAT

$('input:checkbox').click(function () {
    $(this).closest('tr').find('input:checkbox').prop('checked', false);
    $(this).prop('checked', true);
});

However, I have a business requirement in which I'm told if someone clicks on a checkbox that is already checked, that it must uncheck. Yes I understand that radio buttons typically are used in the "only allow 1 item to be selected" however even a radio button is not something you can click to "de-select"

Checkboxes are more natural to be able to un-select but with my function I was trying unselect if already selected and clicked and it always goes to the else statement

$('input:checkbox').click(function () {
    //alert('c');
  $(this).closest('tr').find('input:checkbox').prop('checked', false);

  if ($(this).checked)
  {
      alert('f');
      $(this).prop('checked', false);
  }
  else {
      alert('t');
      $(this).prop('checked', true);
  }

});




Aucun commentaire:

Enregistrer un commentaire