Get number of checkboxes that are checked in Javascript

前端 未结 6 1538
有刺的猬
有刺的猬 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:40

    $('form :checkbox:checked').length

    0 讨论(0)
  • 2020-12-10 15:40
     var checkBoxs = $('#myForm').children('input[type="checkbox"]:checked');
     alert(checkBoxs.length);
    
    0 讨论(0)
  • 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());
    
    0 讨论(0)
  • 2020-12-10 15:51

    vary long way

    you have to give the class name to checkbox and do

    var chkLength = $('input.className:checked').length;

    alert(chkLength);

    this will gove all checked checkBoxes from list of checkbox

    0 讨论(0)
  • 2020-12-10 15:54

    Try

    $(":checkbox").filter(":checked").size()
    
    0 讨论(0)
  • 2020-12-10 15:57
    var chk = $('form').find('input[type=checkbox]:checked').length
    
    0 讨论(0)
提交回复
热议问题