返回" true"如果所有td.has_pais_dist都有文本,否则返回" false"

时间:2014-12-04 03:34:11

标签: javascript jquery html

我试图检查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

2 个答案:

答案 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