JQuery: Verify if in a collection of inputs (of type checkbox) one is checked

后端 未结 4 409
误落风尘
误落风尘 2021-01-25 00:05

I have collection of inputs of type checkbox

var collection = $(\'.className\');
console.log(collection);

result is:

[span.clas         


        
相关标签:
4条回答
  • 2021-01-25 00:12

    You can use the :checked selector, and then check the length property to see if any elements were matched:

    if ($(".className:checked").length) {
        //At least 1 is checked!
    }
    

    However, your output looks like you have span elements, not input elements. If the checkboxes are descendants of the .className elements, add a space before the :checked selector.

    0 讨论(0)
  • 2021-01-25 00:19

    You could test for checked: $('.className:checked');

    0 讨论(0)
  • 2021-01-25 00:34
    $('.className input:checked').length > 0
    

    I'm assuming your checkbox is inside a span since .className is giving you an array of spans.

    0 讨论(0)
  • 2021-01-25 00:36
    if ($('.className input[type="checkbox"]:checked').length>0){
       console.log("At least 1 selected!");
    }
    
    0 讨论(0)
提交回复
热议问题