2014-12-04 114 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 
相關問題