0

如果父 td 中的每个 .itemToFilter 子项都未通过测试(因此返回全部 TRUE),则应该执行 alert('hello world')。但事实并非如此!

第一个 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');
    }

});​
4

2 回答 2

3

返回 insidetrue只是each继续下一次迭代。您将需要执行以下操作:

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');
}
于 2012-08-30T02:11:22.787 回答
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');
    }
});
于 2012-08-30T02:12:39.947 回答