mercredi 13 janvier 2016

check / uncheck checkboxes the easy way

So I want to be able to check 2 checkboxes, 1 "status box" and 1 "status-update box" And I've tried the convenient way of doing this,

using for example this code:

$("input:checkbox").on('click', function () {
  var $box = $(this);
  if ($box.is(":checked")) {
    var group = "input:checkbox[name='" + $box.attr("name") + "']";
    $(group).prop("checked", false);
    $box.prop("checked", true);
  } else {
    $box.prop("checked", false);
  }
});

But this only worked in my JsFiddle, not in my applikation, I'm not sure if it's because I'm using a modal-dialog or not, but it's not working - unknown reason

So I figured I'd do it the rookie way, like this:

    if ($('.checkboxSanction').prop('checked')) {
        $('.checkboxPep').prop('checked', false);
        $('.checkboxUnderage').prop('checked', false);
    }
    if ($('.checkboxPep').prop('checked')) {
        $('.checkboxSanction').prop('checked', false);
        $('.checkboxUnderage').prop('checked', false);
    }
    if ($('.checkboxUnderage').prop('checked')) {
        $('.checkboxSanction').prop('checked', false);
        $('.checkboxPep').prop('checked', false);
    }
    if ($('.checkboxInvestigating').prop('checked')) {
        $('.checkboxClosed').prop('checked', false);
    }
    if ($('.checkboxClosed').prop('checked')) {
        ('.checkboxInvestigating').prop('checked', false);
    }

But here I have to uncheck 1 textbox before being able to re-check one of the "checked, false" ones.

Do you guys have any ideas of how to do this in a rookie way that's bullet proof? - maybe not the best way but it works, if you know what I mean?

Thanks for helping!




Aucun commentaire:

Enregistrer un commentaire