2012-08-30 58 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'); 
    } 
});