How to pick up a clicked multidimensional checkbox

Asked

Viewed 239 times

2

I have a checkbox group and I need to get the checkbox that the user clicked, with this, I need to manipulate others who 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

On 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);

        });
    }
});

Only in this new listing I have this multidimensional array and so it doesn’t work, does anyone have any tips to help me?

  • 1

    Try to change $('input[name="grupo[]"]') for $('input[name^="grupo["]') and see if that solves your problem.

  • It didn’t work. I’m not sure what the ^ and another, I think that way will not catch the group that I still need to manipulate grupo[x][y][]

  • o is to say a name that starts with group[, in case he should take all elements with that name...

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>
<input type="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

  • I’m not doing it like that, first because you’re ready to save it on the server side and I can’t change that part at that point in the championship, and then again, how would I know what group’s value is.

  • I changed the code above, maintaining the properties name as they were, that is, keeping the code to save on the server. Click on "Execute code snippet" and you will see that the client side is working as well. I believe this solution already suits you.

  • It did work. I missed opening my mind a little to what I had been through.. Valew

Browser other questions tagged

You are not signed in. Login or sign up in order to post.