How can get length on checked checkbox array javascript

后端 未结 11 577
旧巷少年郎
旧巷少年郎 2020-12-29 08:16
相关标签:
11条回答
  • 2020-12-29 09:10
    var fobj = document.forms[0];
    
    var c = 0;
    for (var i = 0; i < formobj.elements.length; i++)
    {
    if (fobj.elements[i].type == "checkbox")
    {
    if (fobj.elements[i].checked)
    {
    c++;
    }
    }       
    }
    
    alert('Total Checked = ' + c);
    
    0 讨论(0)
  • 2020-12-29 09:14

    If you want to use plain javascript

    var checkbox = document.getElementsByName('ckbox[]');
    var ln = 0;
    for(var i=0; i< checkbox.length; i++) {
        if(checkbox[i].checked)
            ln++
    }
    alert(ln)
    
    0 讨论(0)
  • 2020-12-29 09:14

    Try this,

    var ch_n = $( "input:checked" ).length;

    0 讨论(0)
  • 2020-12-29 09:15
    var len = $("[name='cbox[]']:checked").length;
    

    will work but will not work if you are directly comparing like

    if ( $("[name='cbox[]']").length= $("[name='cbox[]']:checked").length)
    
    0 讨论(0)
  • 2020-12-29 09:17

    You may use as below as well

    $('[name=cbox\\[\\]]:checked').length
    
    0 讨论(0)
提交回复
热议问题