How to get a multidimensional checkbox clicked

2

I have a checkbox group and I need to get the checkbox that the user clicked, so I need to manipulate the others that are from the same group.

<input type="checkbox" name="grupo[1][1][]" value="A" data-unique="0"> A
<input type="checkbox" name="grupo[1][2][]" value="B" data-unique="0"> B
<input type="checkbox" name="grupo[1][3][]" value="C" data-unique="0"> C
<input type="checkbox" name="grupo[1][4][]" value="D" data-unique="1"> D

<input type="checkbox" name="grupo[2][1][]" value="A" data-unique="0"> A
<input type="checkbox" name="grupo[2][2][]" value="B" data-unique="0"> B
<input type="checkbox" name="grupo[2][3][]" value="C" data-unique="0"> C
<input type="checkbox" name="grupo[2][4][]" value="D" data-unique="1"> D

In another checkbox listing I have done so:

$('input[name="grupo[]"]').click(function() {

    var $this = $(this);
    var is_unique = $this.data('unique');
    var is_checked = $this.is(':checked');
    var grupo = $('input[name="grupo[]"]');

    if(is_unique == '1') {

        grupo.each(function() {
            $(this).prop("checked", false);
            $(this).prop("disabled", is_checked);

            $this.prop("checked", is_checked);
            $this.prop("disabled", false);

        });
    }
});

In my new listing I have this multidimensional array and that way it does not work, does anyone have any tips to help me?

    
asked by anonymous 27.04.2015 / 15:45

1 answer

1

Why not use classes?

$('.grupo').click(function() {
    
    var $this = $(this);
    var is_unique = $this.data('unique');    
    var is_checked = $this.is(':checked');
    var grupo = $this.data('group');
    
    if(is_unique == '1') {        
        $('.grupo').each(function() {
            var grupoAtual = $(this).data('group');
            if(grupoAtual != grupo)
                return;
            
            $(this).prop("checked", false);
            $(this).prop("disabled", is_checked);

            $this.prop("checked", is_checked);
            $this.prop("disabled", false);

        });
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="checkbox" class="grupo" name="grupo[1][1][]" data-group="1" value="A" data-unique="0"> A
<input type="checkbox" class="grupo" name="grupo[1][2][]" data-group="1" value="B" data-unique="0"> B
<input type="checkbox" class="grupo" name="grupo[1][3][]" data-group="1" value="C" data-unique="0"> C
<input type="checkbox" class="grupo" name="grupo[1][4][]" data-group="1" value="D" data-unique="1"> D

<input type="checkbox" class="grupo" name="grupo[2][1][]" data-group="2" value="A" data-unique="0"> A
<input type="checkbox" class="grupo" name="grupo[2][2][]" data-group="2" value="B" data-unique="0"> B
<input type="checkbox" class="grupo" name="grupo[2][3][]" data-group="2" value="C" data-unique="0"> C
<input type="checkbox" class="grupo" name="grupo[2][4][]" data-group="2" value="D" data-unique="1"> D
    
27.04.2015 / 16:28