2012-08-30 4 views
0

부모 td에있는 각 .itemToFilter 자식이 테스트에 실패하면 (즉, 모두 TRUE를 반환하는 경우) alert ('hello world')가 실행되어야합니다. 그러나 그렇지 않습니다!jQuery : 자식 및 부울 값 반복

첫 번째 IF 문이 올바르게 작동하고 있으며 경고로 테스트를 마쳤습니다. 하지만 두 번째는 아닙니다.

var businessTypePullDownValue = $('.businessTypePullDown').val(); 

$('.businessTypeRow td').each(function() { 

    var foundOne = $(this).children('.itemToFilter').each(function() {     

     if(($(this).attr('value') == businessTypePullDownValue)) { 
      return true; 
     } 
    }); 

    if(!foundOne) { 
     alert('hello world'); 
    } 

});​ 
+0

소스 HTML을 제공하십시오 : 당신이 뭔가를 할 필요가있을 것이다 – ianace

답변

3

eachtrue 반환 단순히 다음 반복을 계속한다.

var foundOne = false; 

$(this).children('.itemToFilter').each(function() {     

    if(($(this).attr('value') == businessTypePullDownValue)) { 
     foundOne = true; 
     return false; // break the loop 
    } 
}); 

if(!foundOne) { 
    alert('hello world'); 
} 
1
$('.businessTypeRow td').each(function() { 
    // get child element which class is itemToFilter and 
    // value equals to businessTypePullDownValue 
    var $elements = $('.itemToFilter[value="' + businessTypePullDownValue + '"]', this); 

    if($elements.length > 0) { 
     alert('Hello world'); 
    } 
});