页面加载后计算元素一次

时间:2014-06-27 20:31:09

标签: javascript jquery

我目前正在将一个值从search.php传递给filtering.php。我正在使用$_GET来实现这一目标。在filtering.php页面中,我有一个表格,可根据输入文本框中输入的单词自动过滤。我在URL中传递了值:http://holaweblearning.co.nf/php_learning/filtering.php?key=Dragoo。获取URL中的值并将其放在输入文本框中并显示结果。我能够运行一个函数,根据单词计算过滤结果。问题:结果计数已关闭,它最初显示正确的值,但点击结果后会添加1DEMO

$(document).ready(function($) {

//Trigger key up on search box to show results for word
$('input#filter').trigger('keyup');

//Counts Results
function result_count(){
      var text =  $('.footable tbody tr:not(.footable-filtered)').length;
      $('h5#result_count').text('Number of Results: '+text);
    }
result_count();

//Run on page load
window.setInterval(result_count, 100);

});

1 个答案:

答案 0 :(得分:3)

当您点击某个结果时,会添加一个新行,如下所示,由于这种类型的行在计数中 NOT BEING EXCLUDED ,您的计数将增加一个。

<tr class="footable-row-detail"><td class="footable-row-detail-cell" colspan="3"><div class="footable-row-detail-inner"><div class="footable-row-detail-row"><div class="footable-row-detail-name">Job Title:</div><div class="footable-row-detail-value">Traffic Court Referee</div></div><div class="footable-row-detail-row"><div class="footable-row-detail-name">DOB:</div><div class="footable-row-detail-value">22 Jun 1972</div></div></div></td></tr>

可能你的功能应该是:

function result_count(){
      var text =  $('.footable tbody tr:not(.footable-filtered,.footable-row-detail)').length;
      $('h5#result_count').text('Number of Results: '+text);
}

所以它不计算这一行。