2

我正在尝试计算 yes 的数量,直到由 :contains(certain sring) 指定的特定跨度。我基本上只是想搜索这组问题的前半部分,看看有多少是的。这是我尝试过的代码:

<div class="ce-modal-container">
    <div class="ce-cf-container">
      <span class="ce-cf-html-label">Have you chosen your exit path and/or successor?:</span>
      <span class="ce-cf-html-field"> Yes</span>
    </div>
    <div class="ce-cf-container">
      <span class="ce-cf-html-label">Have you chosen your exit path and/or successor?:</span>
      <span class="ce-cf-html-field"> No</span>
    </div>
    <div class="ce-cf-container">
      <span class="ce-cf-html-label">Have you done well this quarter?:</span>
      <span class="ce-cf-html-field"> No</span>
    </div>
    <div class="ce-cf-container">
      <span class="ce-cf-html-label">Have you chosen your exit path and/or successor?:</span>
      <span class="ce-cf-html-field"> Yes</span>
    </div>
    <div class="ce-cf-container">
      <span class="ce-cf-html-label">Have you chosen your exit path and/or successor?:</span>
      <span class="ce-cf-html-field"> Yes</span>
    </div>
</div>

<script>
var spanYes = $("span.ce-cf-html-field:contains('Yes')"),
    secondHalf = $("span.ce-cf-html-label:contains('done well')"),
    searchtxtall = spanYes.length;
    searchFirstHalf = $(".ce-cf-container").nextUntil(secondHalf).spanYes.length;
console.log(searchtxtall); //should return 3
console.log(searchFirstHalf); //should return 1
</script>

在此先感谢我知道这是很多代码。

4

4 回答 4

2

一些东西:

  • 你拼错了“容器”
  • 为什么.nextUntil().spanYes呢?没有意义/.spanYes什么都不做
  • .nextUntil与兄弟姐妹一起工作,但.ce-cf-container它是容器。尝试:

    $(".ce-cf-container span").nextUntil(secondHalf).is(spanYes);

于 2012-12-02T17:56:21.700 回答
0

感谢您的帮助,但这是我找到的解决方案。

var $secondHalf = $(".ce-modal-container .ce-cf-container:lt(2)"),
    $searchFirstHalf = $secondHalf.find($spanYes).length;

如果你想知道。

于 2012-12-02T18:58:13.093 回答
0

另一个不太花哨的解决方案:

var count = 0;
$(".ce-cf-container").each(function () {
   if ($(".ce-cf-html-label:contains(done well)", this).length) {
      //break out of loop
      return false;
   }
   if ($(".ce-cf-html-field:contains(Yes)", this)).length) {
      count++;
   }
});
于 2012-12-02T19:00:37.640 回答
0

我可能会放弃更多,而不是依赖 jQuery 选择器。例如:http: //jsfiddle.net/hRVQK/

$(document).ready(function() {
    var answers = [];
    $('.ce-cf-html-field').each(function () {
        answers.push($(this).text().indexOf('Yes') != -1);
    });
    var half = Math.round(answers.length/2);
    var answersFirstHalf = answers.slice(0, half);

    var countFirstHalf = 0;
    $(answers.slice(0, half)).each(function () {
        if (this == true)
            countFirstHalf++;
    });

    var count = 0;
    $(answers).each(function() {
        if (this == true)
            count++;
    });

    console.log('count', count, 'count first half', countFirstHalf);
});

​</p>

于 2012-12-02T19:06:31.693 回答