2014-12-04 217 views
0

我试图检查td.has_pais_dist是否具有文本(含义值)。我在做这个瓦乌:如果所有td.has_pais_dist都有文本,则返回“true”,否则返回“false”

$(document).ready(function(){ 
    var activateBtnNext = $("#distribuidorBody").find(".has_pais_dist").filter(function() { 
    return $.trim($(this).text()).length > 0; 
    }).length > 0; 

    var boolVar = $('#distribuidorBody .has_pais_dist:empty').length > 0; 

    console.log(activateBtnNext); 
    console.log(boolVar); 
}); 

在这样一组HTML元素:

<table id="contenedorDistribuidores"> 
    <thead> 
     <tr class="tableHead"> 
     <th>Nombre</th> 
     <th>País</th> 
     </tr> 
    </thead> 
    <tbody id="distribuidorBody"> 
     <tr> 
     <td>kksddfkdfkdf</td> 
     <td id="distTd-1" class="has_pais_dist"></td> 
     </tr> 
     <tr> 
     <td>dfgdfgdfg</td> 
     <td id="distTd-2" class="has_pais_dist"></td> 
     </tr> 
     <tr> 
     <td>fsdfkjdsf</td> 
     <td id="distTd-3" class="has_pais_dist">Some text</td> 
     </tr> 
    </tbody> 
    </table> 

但两者的回报“真”,这是错误的,因为前两个TD还没有文本值。我做错了什么?你可以看看here

+0

你测量2个不同的东西 – charlietfl 2014-12-04 03:43:19

+0

@charlietfl我试图做同样的两种方式我忘了提, – ReynierPM 2014-12-04 03:43:57

+0

的点显示是不是想达到同样的目标2次测试。不是100%清楚你的整体目标是什么。全部没有文字或者没有文字? – charlietfl 2014-12-04 03:47:06

回答

1

你需要的,如果没有空的元素,因此有真正

var boolVar = $('#distribuidorBody .has_pais_dist:empty').length == 0; 

var boolVar = $('#distribuidorBody .has_pais_dist').is(':empty') 
0

不知道你需要什么样的结果。我猜你需要有文字的过滤物品。如果这是正确的,只需从您的过滤器功能中删除.length > 0

var activateBtnNext = $("#distribuidorBody").find(".has_pais_dist").filter(function() { 
     return $.trim($(this).text()).length > 0; 
    });//.length > 0; this is setting your var to a bool, instead of keeping the filtered elements 

    console.log(activateBtnNext); // this is an array with the las element that does have text 
    console.log(activateBtnNext.length); // this is the number of elements that have text, in this case 1 
    console.log(activateBtnNext[0]); // this is the element that does have text 
相关问题