1

ここに私のJqueryがあります

$th.each(function(idx) {
      var notSelected = $(this).text();
      if ((notSelected.indexOf(to) != -1) || (notSelected.indexOf(from) != -1)) {

      if (idx < 10)
      {
         $(this).show();
         // and show its corresponding td
         $td.eq(idx).show();
      }
      }
    }); 

これは、HTML テーブルの tableFilter タイプ関数の一部です。ただし、結果の2つだけを表示したいです。ある種のインデックス カウンターをインスタンス化しようとしましたが、失敗しました。どんな助けや考えもいただければ幸いです。

4

2 に答える 2

1
var index = 0;
$th.each(function(idx) {
      var notSelected = $(this).text();
      if ((notSelected.indexOf(to) != -1) || (notSelected.indexOf(from) != -1)) {

      if (idx < 10 && index < 2)
      {
         $(this).show();
         // and show its corresponding td
         $td.eq(idx).show();
         index = index + 1;
      }
      }
    }); 
于 2010-05-03T18:04:17.427 に答える
0

これは、受け入れられている回答よりも効率的であり、名前空間を汚染しません。

(function(index){
    $th.each(function(idx) {
        if(idx < 10){
            var self = $(this),
                notSelected = self.text();
            if ( notSelected.indexOf(to) > -1 || notSelected.indexOf(from) > -1 ) 
                self.show();
                // and show its corresponding td
                $td.eq(idx).show();
                if(++index===2){
                    //break the loop
                    return false;
                }
            }
        }
    });
}(0));
于 2010-05-03T18:38:11.847 に答える