Check all classbuttons daughter radiobuttons

0

I have a page that will display a checklist, the data displayed comes from the database. I need that when the user clicks on the first checkbox (id="selectTASA", for example) it select all the radii corresponding to the value A, as well as the corresponding checkboxes. That I could do.

My problem is that each header (example Front) has 4 checkboxes, their function is to only mark the "children" radios of this header, that is, when clicking on the checkbox, it will only mark the radios connected to them, currently the code marks all of the class "column1", because I could not define a selector that would give me this. Here is the code:

<html>
<head>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="bootstrap.min.css">

    <style>
        table, table tr, table td{
            border: 1px solid black;
        }   

        td{
            width: 90px;    
        }   

        div.divInline{
            display: inline-block;
            margin-left: 30px;
        }
    </style>
</head>
<body>
<?php global $x; ?>
<div>
    <input type="checkbox" class="selecionarTdsA" id="selecionaTdsA">Selecionar todas a
    <input type="checkbox" class="selecionarTdsB" id="selecionarTdsB">Selecionar todas b
    <input type="checkbox" class="selecionarTdsC" id="selecionarTdsC">Selecionar todas c
    <input type="checkbox" class="selecionarTdsD" id="selecionarTdsD">Selecionar todas d
</div>

<?php $meuCabecalho = ['Fachada', 'Calçada', 'Telhado', 'Equipamentos']; ?>

<?php for($q = 0; $q < 4; $q++){ ?>

<div class="panel-group" id="accordion">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a data-toggle="collapse" data-parent="#accordion" href="#collapse<?php echo $q ?>">
            <?php echo $meuCabecalho[$q] ?>
        </a>    
        <div class="divInline">
            <input type="checkbox" class="<?php echo $meuCabecalho[$q] ?>1" id="selecionarCols1" value="check1">Selecionar a
            <input type="checkbox" class="<?php echo $meuCabecalho[$q] ?>2" id="selecionarCols2" value="check2">Selecionar b
            <input type="checkbox" class="<?php echo $meuCabecalho[$q] ?>3" id="selecionarCols3" value="check3">Selecionar c
            <input type="checkbox" class="<?php echo $meuCabecalho[$q] ?>4" id="selecionarCols4" value="check4">Selecionar d
        </div>
      </h4>
    </div>
    <div id="collapse<?php echo $q ?>" class="panel-collapse collapse">
      <div class="panel-body">
        <table>
            <tbody>

             <?php for($a = 0; $a <5; $a++){ ?>
                <tr>
                    <td>Subitem <?php echo $a ?></td>
                    <td><input type="radio" name="subcabecalho<?php echo $x ?>" value="a" class="coluna1">a</td>
                    <td><input type="radio" name="subcabecalho<?php echo $x ?>" value="b" class="coluna2">b</td>
                    <td><input type="radio" name="subcabecalho<?php echo $x ?>" value="c" class="coluna3">c</td>
                    <td><input type="radio" name="subcabecalho<?php echo $x ?>" value="d" class="coluna4">d</td>            
                </tr>

             <?php  $x++; ?>    
             <?php } ?> 
            </tbody>

            <tr>

            </tr>
        </table>
      </div>
    </div>
  </div>
  <?php } ?>    
</div>

<script src="jquery.min.js"></script>   

<script src="bootstrap.min.js"></script>

<script>

$(document).ready(function() {
    $('input:checkbox[id="selecionaTdsA"]').click(function() {
        if($("input[id='selecionaTdsA']").is(':checked')) {

            $(".coluna1").each(function () {
                $(this).prop("checked", true);
            });     

            $('input:checkbox[id=selecionarCols1]').prop('checked', true);

        }else{
            $("input[class='coluna1']").each(function () {
                $(this).prop('checked', false);
            }); 

            $('input:checkbox[id=selecionarCols1]').prop('checked', false);
        }
    });


    //AQUI TENHO QUE COLOCAR O SELETOR CORRETO
    $('input:checkbox[id="selecionarCols1"]').click(function() {
        $(".coluna1").each(function () {
            $(this).prop("checked", true);
        });     

    });




});



</script>

</body>

    
asked by anonymous 11.06.2018 / 15:37

1 answer

1

Use the JQuery date attribute . With it you can add more information to your elements, in addition to the already used id's and classes. You can, for example, put a "data-item" attribute (it must always be data- {identifier}) in the checkboxes of each header, identified with the number or the shape that you want. Then in the radios you also put date attributes, in order to select which should be marked, you can compare the attribute of the checkbox with those of the radios. It would look something like this:

$(document).ready(function(){
        // Seleciona elementos cuja classe começa com 'subcabecalho-'
		$("[class^='subcabecalho-']").on("change", function(){
			// Pega o atributo data-item do checkbox, pra saber de qual item(cabeçalho) iremos marcar as colunas 
            var item = $(this).data("item");
            // Usamos o valor do checkbox pra saber qual coluna será marcada
			var valor = $(this).val();
        // Seleciona todos os radios com a classe radio-item{numero_do_cabecalho} e que tenham o atributo data = valor concatenado com item, ex: a1, b2, d1... E seta a propriedade "checked" de acordo com o que foi marcado no checkbox
    	$(".radio-item"+item+"[data-item='"+valor+item+"']").prop("checked", $(this).prop("checked"));
    });
    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="item1">
<h3>
Item 1
</h3>
<input type="checkbox" class="subcabecalho-item1" data-item="1" value="a"> a
<input type="checkbox" class="subcabecalho-item1" data-item="1"  value="b"> b 
<input type="checkbox" class="subcabecalho-item1" data-item="1"  value="c"> c
<input type="checkbox" class="subcabecalho-item1" data-item="1"  value="d"> d

<table>
<tr>
  <td>Subitem 1</td>
  <td><input type="radio" class="radio-item1" data-item="a1" value="a"></td>
  <td><input type="radio" class="radio-item1" data-item="b1"  value="b"></td>
  <td><input type="radio" class="radio-item1" data-item="c1" value="c"></td>
  <td><input type="radio" class="radio-item1" data-item="d1" value="d"></td>
</tr>
<tr>
  <td>Subitem 2</td>
  <td><input type="radio" class="radio-item1" data-item="a1" value="a"></td>
  <td><input type="radio" class="radio-item1" data-item="b1" value="b"></td>
  <td><input type="radio" class="radio-item1" data-item="c1" value="c"></td>
  <td><input type="radio" class="radio-item1" data-item="d1" value="d"></td>
</tr>
<tr>
  <td>Subitem 3</td>
  <td><input type="radio" class="radio-item1" data-item="a1" value="a"></td>
  <td><input type="radio" class="radio-item1" data-item="b1" value="b"></td>
  <td><input type="radio" class="radio-item1" data-item="c1" value="c"></td>
  <td><input type="radio" class="radio-item1" data-item="d1" value="d"></td>
</tr>
</table>
</div>
<div id="item2">
<h3>
Item 2
</h3>
<input type="checkbox" class="subcabecalho-item2" data-item="2" value="a"> a
<input type="checkbox" class="subcabecalho-item2" data-item="2" value="b"> b 
<input type="checkbox" class="subcabecalho-item2" data-item="2" value="c"> c
<input type="checkbox" class="subcabecalho-item2" data-item="2" value="d"> d

<table>
<tr>
  <td>Subitem 1</td>
  <td><input type="radio" class="radio-item2" data-item="a2" value="a"></td>
  <td><input type="radio" class="radio-item2" data-item="b2" value="b"></td>
  <td><input type="radio" class="radio-item2" data-item="c2" value="c"></td>
  <td><input type="radio" class="radio-item2" data-item="d2" value="d"></td>
</tr>
<tr>
  <td>Subitem 2</td>
  <td><input type="radio" class="radio-item2" data-item="a2" value="a"></td>
  <td><input type="radio" class="radio-item2" data-item="b2" value="b"></td>
  <td><input type="radio" class="radio-item2" data-item="c2" value="c"></td>
  <td><input type="radio" class="radio-item2" data-item="d2" value="d"></td>
</tr>
<tr>
  <td>Subitem 3</td>
  <td><input type="radio" class="radio-item2" data-item="a2" value="a"></td>
  <td><input type="radio" class="radio-item2" data-item="b2" value="b"></td>
  <td><input type="radio" class="radio-item2" data-item="c2" value="c"></td>
  <td><input type="radio" class="radio-item2" data-item="d2" value="d"></td>
</tr>
</table>
</div>
    
11.06.2018 / 17:24