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