JQuery implements statistics on the number of checkboxes selected

  • 2020-03-30 04:23:45
  • OfStack

The code is very brief, the function is also very simple, but very practical, here is not detailed directly on the code

JQuery code:


        function countNum(){
          //alert($('input[name=check]:checked').length);
         var arrays = new Array();
          var items = document.getElementsByName("check");
          for(var i=0;i<items.length;i++){
              if(items[i].checked){
                  arrays.push(items[i].value);
              }
          }
          alert(arrays.length);
        }

HTML code:


    <input type="checkbox" value="1" name="check" checked/>
    <input type="checkbox" value="2" name="check"/>
    <input type="checkbox" value="3" name="check"/>
    <input type="button" value=" Select the number of " id="btn" onclick="countNum();">

If you have the same requirements in the project, please refer to it. If you have a better way, please contact me.


Related articles: