1

I have a menu consisting of sort criteria. The options per criteria are listed as check boxes in a div wioth class '.collapse_box'.

I need to check each of these div's to see if any of the checkboxes it contains are checked. If there are any checkedboxes I need to set the DIV display to block.

I was thinking along these lines:

$('.collapse_box')each(function()

    if( $(this).(input:checked).length() > 0{  //here lies my problem
        $(this).show();
    }

});

Seeing that I am very new to javascript and jquery I don't know how to return the checked boxes for $(this). Or better said: the correct method to check if any checkboxes in $(this) are checked.

4

2 回答 2

5

您的选择器错误,元素始终以字符串或对象的形式给出:

if( $(this).find('input:checked').length > 0){

另外,length不是函数,而是属性。你忘记了.find()
Made you a jsFiddle with demo

于 2013-10-14T18:31:18.127 回答
1

好吧,假设inputs 是孩子,这样的事情会起作用:

$('.collapse_box').each(function(){
    if($(this).find('input').prop('checked')){
        $(this).show();
    }
});

.prop('checked')片段返回一个布尔值,它是否input被选中。

编辑Martijn 提出了一个很好的观点,您可以将其切换到 vanilla JS,并为选择器添加一个 mod。

$('.collapse_box').find('input').each(function(){
    var self = this;
    if(self.checked){
        self.style.display = 'block';
    }
});
于 2013-10-14T18:32:53.290 回答