Get number of checkboxes that are checked in Javascript

前端 未结 6 1552
有刺的猬
有刺的猬 2020-12-10 15:13

I am trying to make a javascript function (although jquery is perfectly OK) that will return a number that corresponds to the number of checkboxes checked in a form. Seems

6条回答
  •  我在风中等你
    2020-12-10 15:44

    Try this:

    var formobj = document.forms[0];
    
    var counter = 0;
    for (var j = 0; j < formobj.elements.length; j++)
    {
        if (formobj.elements[j].type == "checkbox")
        {
            if (formobj.elements[j].checked)
            {
                counter++;
            }
        }       
    }
    
    alert('Total Checked = ' + counter);
    

    .

    With JQuery:

    alert($('form input[type=checkbox]:checked').size());
    

提交回复
热议问题