2016-06-19 8 views
0

私は以下のコードを持っています。 #no-results要素は、結果が見つからない場合にのみ表示される必要があります。現在はIFの検索結果があります。jquery show div検索結果が返されない場合のみ

$('#no-results').hide(); 
     $('#video-search-text').keyup(function() { 
      $('.video-search').hide(); 
      $('#no-results').css("display", "none"); 
      var txt = $('#video-search-text').val(); 

      $('.video-search').each(function() { 
       if ($(this).text().toUpperCase().indexOf(txt.toUpperCase()) != -1) { 
        $(this).show(); 

       } 
       else { 
         $('#no-results').css("display", "block"); 

       } 
      }); 

     }); 

私は、便宜上、以下のJSFiddleを作成しました。ありがとう

答えて

0

forループごとに結果がないかどうかのチェックを削除し、検索が完了したら配置します。 like

$('#no-results').hide(); 
$('#video-search-text').keyup(function() { 
    $('.video-search').hide(); 
    $('#no-results').css("display", "none"); 
    var txt = $('#video-search-text').val(); 
    var resultCount = 0; 
    $('.video-search').each(function() { 
     if ($(this).text().toUpperCase().indexOf(txt.toUpperCase()) != -1) { 
      $(this).show(); 
      resultCount++; 
     } 
    }); 

    if (resultCount == 0) { 
     $('#no-results').css("display", "block"); 
    } 

}); 
+0

完璧!ありがとう! – James

+0

喜んでお手伝いします:)これで問題が解決した場合は、それを承認済みとしてマークしてください。 –

0

Jamesはあなたに良い解決策を与えました。 は基本的に、問題が

$('.video-search').each(function() 

は、あなたのjsFiddleで「ABC」を検索したとこのループから来て、それは最初<h1>との一致を見つけるでしょう。これは最初のブロックに入るので、 '見つからなかった結果'は表示されません。

しかし、各要素についてループしているので、 'abc'と 'def'を比較し、elseブロックに入ります。 「ghi」と比較して同じことが起こります。

関連する問題