Query classnames of checked items as array?

This question already has an answer here:

  • jQuery to loop through elements with the same class 14 answers

  • You need to iterate all the :checked checkboxes, here you can use .map()

    var clss=$("[class*='observedType']:checked").map(function(){
       return $(this).attr('class')
    }).get();
    

    Updated Fiddle


    I would recommend you to use custom data-* prefix custom attribute to store the arbitrary data.

    <input type="checkbox" id="cb_31" class="observedType" data-type="observedType1">
    

    Which can be retrieved using .data() method

    var clss = $(".observedType:checked").map(function() {
      return $(this).data('type')
    }).get();
    

    With Custom attribute Fiddle


    您可以在javascript中执行以下操作:

    var myInputs = document.getElementsByTagName('input');
    var myCheckedInputs = Array.from(myInputs).filter(function(input) {
      return input.checked;
    });
    
    链接地址: http://www.djcxy.com/p/83506.html

    上一篇: $ .each(selector)和$(selector).each()之间的区别是什么?

    下一篇: 将检查的项目的类名称查询为数组?