10

JQuery - Disable the check boxes based on the attribute of data checked at first...

 3 years ago
source link: https://www.codesd.com/item/jquery-disable-the-check-boxes-based-on-the-attribute-of-data-checked-at-first-click.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

JQuery - Disable the check boxes based on the attribute of data checked at first click

advertisements

I have a list of check boxes. I need to disable all other check boxes with a different data-groupid and leave the check boxes with the same data-groupid enabled. When the user unchecks all the check boxes, all the check boxes become available again. Here's a snip of my HTML. The group ID could be any number and is generated dynamically.

<input type="checkbox" value="1" name="mergeTicked[]"  class="pickSheetCheck" data-groupid="1">
<input type="checkbox" value="2" name="mergeTicked[]"  class="pickSheetCheck" data-groupid="2">
<input type="checkbox" value="3" name="mergeTicked[]"  class="pickSheetCheck" data-groupid="1">
<input type="checkbox" value="4" name="mergeTicked[]"  class="pickSheetCheck" data-groupid="1">
<input type="checkbox" value="5" name="mergeTicked[]"  class="pickSheetCheck" data-groupid="2">
<input type="checkbox" value="6" name="mergeTicked[]"  class="pickSheetCheck" data-groupid="1">
<input type="checkbox" value="7" name="mergeTicked[]"  class="pickSheetCheck" data-groupid="4">
<input type="checkbox" value="8" name="mergeTicked[]"  class="pickSheetCheck" data-groupid="3">
<input type="checkbox" value="9" name="mergeTicked[]"  class="pickSheetCheck" data-groupid="3">
<input type="checkbox" value="10" name="mergeTicked[]"  class="pickSheetCheck" data-groupid="6">
<input type="checkbox" value="11" name="mergeTicked[]"  class="pickSheetCheck" data-groupid="6">
<input type="checkbox" value="12" name="mergeTicked[]"  class="pickSheetCheck" data-groupid="6">
<input type="checkbox" value="13" name="mergeTicked[]"  class="pickSheetCheck" data-groupid="1">
<input type="checkbox" value="14" name="mergeTicked[]"  class="pickSheetCheck" data-groupid="1">
<input type="checkbox" value="15" name="mergeTicked[]"  class="pickSheetCheck" data-groupid="3">

And here's how far i've got with the jQuery. I know it errors but i'm nearly there I think. I also need to enable all the check boxes when they are all unchecked.

$(document).ready(function(){
var $inputs = $(".pickSheetCheck");
function checkInput(thisObj){
var datagrp = thisObj.attr("data-groupid");
$(input:not[data-groupid=datagrp]).attr("disabled", true);

alert(datagrp);

}

$inputs.change(function() {
    checkInput($(this));
});
});

It seems simple enough what I'm trying to do but I've been at this for a while!

EDIT:

I've got it working however I could do with some help 'resetting' when no check boxes are selected

$(document).ready(function(){

var $inputs = $(".pickSheetCheck");
function checkInput(thisObj){
var datagrp = thisObj.attr("data-groupid");

$( ".pickSheetCheck" ).not( '[data-groupid="'+datagrp+'"]' ).attr("disabled", true);
}
$inputs.change(function() {
    checkInput($(this));
});

});


To reset the checkboxes you could just count the checked boxes and if there's none, enable all.

if ($('.pickSheetCheck:checkbox:checked').length == 0) {
  $inputs.attr('disabled', false);
} else {
  $( ".pickSheetCheck" ).not( '[data-groupid="'+datagrp+'"]' ).attr("disabled", true);
}

Example.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK