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

牧云@^-^@ 提交于 2020-01-11 11:19:09

问题


I have collection of inputs of type checkbox

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

result is:

[span.className, span.className, span.className]

how to check if there is at least one checked


回答1:


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.




回答2:


if ($('.className input[type="checkbox"]:checked').length>0){
   console.log("At least 1 selected!");
}



回答3:


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




回答4:


$('.className input:checked').length > 0

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



来源:https://stackoverflow.com/questions/12598846/jquery-verify-if-in-a-collection-of-inputs-of-type-checkbox-one-is-checked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!