2011-07-15 10 views
3

私のFAQページに検索機能を追加しようとしています。contains()を使用してdivコンテンツを表示/非表示にするページ内検索

私が欲しいのは、ユーザーがキーワードを入力し、そのキーワードに対してjqueryを実行し、関連するすべての回答のdisplay:blockを設定するテキストボックスです。

私はこれまででてきた何

<form name="searchBox"> 
     Keyword(s): <input type="text" name="keyword" /> 
     <input type="button" value="Search" onClick="searchFunction()" /> 
    </form> 
    <div class="searchable" style="display:none"> 
     This is the first software question and answer.</div> 
    <div class="searchable" style="display:none"> 
     This is the first hardware question and answer.</div> 
    <script type="text/javascript"> 
     function searchFunction() { 
      var searchTerm = document.searchBox.keyword.value; 
      $(" :contains('"+searchTerm+"')").addStyle("display:block"); } 
    </script> 
+0

「searchTerm」の値は何ですか? –

+0

これがなぜ機能していないのかを詳しく教えてください。 –

答えて

6

この

function searchFunction() { 
      var searchTerm = document.searchBox.keyword.value; 
      $(".searchable").each(function(){ 
       $(this).(":contains('"+searchTerm+"')").show(); 
      }); 

} 
0

変更.addStyle( "表示:ブロック")(.SHOWへ)

4

を試してみて、これを試してみてください。

function searchFunction() { 
    $(".searchable") 
     .hide() 
     .filter(":contains('" + $("input[name='keyword']").val() + "')") 
     .show(); 
} 
関連する問題