How can get length on checked checkbox array javascript

后端 未结 11 576
旧巷少年郎
旧巷少年郎 2020-12-29 08:16
相关标签:
11条回答
  • 2020-12-29 08:55

    Try this

    jQuery solution:

    var len = $(":checked",$("input[name='cbox[]']")).size();
    
    0 讨论(0)
  • 2020-12-29 08:57

    jQuery solution:

    var len = $("[name='cbox[]']:checked").length;
    

    JavaScript solution:

    var len = [].slice.call(document.querySelectorAll("[name='cbox[]']"))
        .filter(function(e) { return e.checked; }).length;
    
    0 讨论(0)
  • 2020-12-29 08:58

    $('input:checked').length will do if you do not have any other input tags than in this form.

    0 讨论(0)
  • 2020-12-29 09:04

    Try out,

    var boxes = $('input[name="cbox[]"]:checked');
    

    find how many are checked,

    $(boxes).size();
    

    or

    $(boxes).length();
    
    0 讨论(0)
  • 2020-12-29 09:05

    you also do by this

    you have to define class for checkBox and then follow below

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

    this will print your total no of checkboxes from list

    0 讨论(0)
  • 2020-12-29 09:10

    Doing it with jQuery would shorten the code and make it more readable, maintainable and easier to understand. Use attribute selector with :checked selector

    Live Demo

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