select and count checkbox

2

I can not hit the code below: the total variable does not add to countCheckedCheckboxes. And when you click the mark button all but uncheck some and click the button again that which was marked decollects and what was demarcated is marked.

SCRIPT

var total=0;
function checkAll(theForm, cName) {
    for (i=0,n=theForm.elements.length;i<n;i++){
        if (theForm.elements[i].className.indexOf(cName) !=-1)
            theForm.elements[i].checked = !(theForm.elements[i].checked);   
     }
        total = $('input[type=checkbox]:checked').length;
}

$(document).ready(function(){

    var $checkboxes = $('#checkbox_form td input[type="checkbox"]');

    $checkboxes.change(function(){   
        var countCheckedCheckboxes = $checkboxes.filter(':checked').length;
        countCheckedCheckboxes=(countCheckedCheckboxes+total);

        $('#count-checked-checkboxes').text(countCheckedCheckboxes);
    });

});
    
asked by anonymous 09.11.2016 / 21:48

2 answers

1

I got my intent and I will share the code:

var w=0;
var y=0;
function checkAll(theForm, cName, status) {

for (i=0,n=theForm.elements.length;i<n;i++)
    if (theForm.elements[i].className.indexOf(cName) !=-1) {
      theForm.elements[i].checked = status;
    }
}

    $(document).ready(function(){
        var $checkboxes = $('#checkbox_form td input[type="checkbox"]');
        $checkboxes.change(function(){   

        var meu_array = ['RIhBx','ddwkj','qmCvU','IFRtS','KiLNX','cmSEL','cAFUq','PJkBz','cEpFF','NZJhD','UfVPS','lrvnJ','ftfte','IJFNS'];

        for ( var i = 0; i < meu_array.length; i++ ) {

                $("input:checkbox[class="+meu_array[ i ]+"]").each(function () {
                    if($(this).is(":checked")==true){
                        w=1;
                    }else{
                        y=1;
                    }
                }); 

            if ((w==0) && (y==1)){
                document.getElementById(meu_array[ i ]).checked = false;
            }
            w=0;
        }   

        var tot=$('input[name="songs[]"]:checked').length;
        if (tot==0){
            $('#count-checked-checkboxes').text("");
        }else if (tot==1){
            $('#count-checked-checkboxes').text(tot+" selecionada");
        }else{
            $('#count-checked-checkboxes').text(tot+" selecionadas");
        }

    });
});
    
15.11.2016 / 18:39
0

I think this helps Léo. I used Jquery because I saw that you made use of it and separated the checkboxes by CSS class, because you might want to select and check a particular checkbox set.

$("#btnAll").click(function(){
       // $(":checkbox") Poderia usar este tbm .. só que irá pegar todos os checkbox do seu document
       $(".carro").prop( "checked", true);
       countCarroSelecionados();
});

$("#btnClear").click(function(){
       // $(":checkbox") Poderia usar este tbm .. só que irá pegar todos os checkbox do seu document
       $(".carro").prop( "checked", false);
       countCarroSelecionados();
});

$("#btnShowResult").click(function(){
       countCarroSelecionados();
});

function countCarroSelecionados(){
 var checkBoxs = 	$( ".carro" ).filter(function() {
    return $(this).prop('checked');
  	});
  alert(checkBoxs.length);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><!DOCTYPEhtml><html><body><formaction="">
<button id="btnAll" type="button">
  Selecionar Todos
</button>
<button type="button" id="btnClear">
  Limpar
</button>
<button type="button" id="btnShowResult">
  Resultado
</button>
<br>
<input type="checkbox" class="carro" value="mustang">Mustang
  <input type="checkbox" class="carro" value="camaro">Camaro
  <input type="checkbox" class="carro" value="veloster">Veloster
  
</form>
</body>
</html>
    
09.11.2016 / 22:43