我试图检查td.has_pais_dist
是否有文字(含义价值)。我在这个wau做的事情:
$(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>
但两者都返回&#34; true&#34;这是错误的,因为前两个TD没有文本值。我做错了什么?你可以看看here。
答案 0 :(得分:1)
如果没有空元素,则需要为true
var boolVar = $('#distribuidorBody .has_pais_dist:empty').length == 0;
或
var boolVar = $('#distribuidorBody .has_pais_dist').is(':empty')
答案 1 :(得分: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