There are a lot of topics about javascript and conditional checking checkboxes, but I have not found one that matchs my case.
My usecase: I have generated a number of rows with checkboxes each row, lets say the number is n. Now the user should be able to check only n-1 of them. So in other words when we have 4 checkboxes and 3 of 4 are checked, the last one should be disabled. When only 2 of 4 are checked then all checkboxes are available.
I have tried:
var $checks = $('input:checkbox').click(function (e) {
var numChecked = $checks.filter(':checked').length;
var lastUnchecked = $checks.prop('checked', false);
if (numChecked = $checks.length - 1) {
lastUnchecked.disabled = true;
}
else {
//enable checkbox that was disabled before
lastUnchecked.disabled = false;
}
});
but this doesn't work because var lastUnchecked = $checks.prop('checked', false); return all checkboxes, not only unchecked
Aucun commentaire:
Enregistrer un commentaire