vendredi 30 décembre 2016

Repeated selecting of checkboxs based on value

Using Jquery I have code that works to select all the checkboxes of the same value as the checkboxes that is selected. It also disables all checkboxes on the same row as the checkbox that is selected (the checkboxes are within a table).

This is all great. However it is slightly incomplete, and the last piece of the puzzle is eluding me. Checkboxes on the rows that are disabled are, by definition, disabled. However, it would be perfect if all checkboxes with the same value as those disabled were also disabled. This is to prevent users from selecting incompatible items (there's also a server side measure to prevent this, but client side is so much more pretty and user-friendly).

JSFiddle: http://ift.tt/2ikR5Ii $(".chkbox").on('change', function() {

    var currentObj = $(this);
    var val = $(this).val();
    if ($(this).is(":checked")) {

        $(":checkbox[value='" + val + "']").prop("checked", true);
        $(this)
            .closest("tr")
            .find(".chkbox").attr("disabled", "disabled");
        $(currentObj).removeAttr("disabled");
        $(":checkbox[value='" + val + "']")
            .closest("tr")
            .find(".chkbox").attr("disabled", "disabled");
        $(":checkbox[value='" + val + "']").removeAttr("disabled");
    } else {
        $(":checkbox[value='" + val + "']").prop("checked", false);
        $(":checkbox[value='" + val + "']")
            .closest("tr")
            .find(".chkbox").removeAttr('disabled');;

    }

});




Aucun commentaire:

Enregistrer un commentaire