2012-02-11 6 views
0

私は自分のモバイルウェブサイトをライブ検索しようとしていますが、ユーザーが手紙を入力するたびにデータベースに問い合わせる必要はありません。私はjQueryでそれをループしています。問題は私が3300の名前を持っていて、それを検索するときにブラウザーをフリーズしています。誰かがそれを行うためのよりよい方法についてのヒントを教えてくれますか?ここに私のコードです:Jqueryスクリプトはブラウザをフリーズしていますが動作しています

$(document).ready(function(){ 
    $("input#search").keyup(function(){ 
     var filter = $(this).val(), count = 0; 
      var html = ""; 
     $("ol.pacientes li").each(function(){ 
        var nome_paciente = $(this).text(); 
        if(nome_paciente.indexOf(filter.toUpperCase()) != -1){ 
         html = html + " " + nome_paciente; 
        } 

        $('#pacientes_hint').html(html); 
     }); 

答えて

0

jQueryオートコンプリートバージョンを使用してください。すべての名前を持つ配列をロードしてオートコンプリートに渡すことができます。これはオンザフライで動作します。

http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/

+0

感謝、ちょうどこれを試して、それは十分速かった、出力をカスタマイズすることに問題があった、ありがとう –

0

あなたがあなたのそれぞれを変更することができます:短いことに加え

var text = $("ol.pacientes li:contains(\""+filter.toUpperCase()+"\")").map(function() { 
    return $(this).text(); 
}).join(' '); 
$('#pacientes_hint').text(text); 

、唯一の改善が役立つ可能性があり、最後にのみ$('#pacientes_hint')の内容を設定します。

より創造的な解決策が必要な場合は教えてください。

+0

入力テキストをエスケープする必要があります: 'filter.toUpperCase()。replace( '"'、 '\\ "')' – ori

0

まず、#pacientes_hintを各機能の外側に移動できます。

$(document).ready(function(){ 
$("input#search").keyup(function(){ 
    var filter = $(this).val(), count = 0; 
     var html = ""; 
    $("ol.pacientes li").each(function(){ 
       var nome_paciente = $(this).text(); 
       if(nome_paciente.indexOf(filter.toUpperCase()) != -1){ 
        html = html + " " + nome_paciente; 
       } // end if 


    }); // end each 
    $('#pacientes_hint').html(html); 

そして、それは毎回それを見ていないので、あなたは、keyUpイベントハンドラの前に変数としてol.pacientesを定義することができ、各関数で、変数内の検索:

$(document).ready(function(){ 
var pacientes_list = $("ol.pacientes"); 
var pacientes_hint = $("#pacientes_hint"); 
$("input#search").keyup(function(){ 
    ... 
    $("li", $(pacientes_list)).each(function(){ // search in the container 
     ...  
    }); // end each 
    $(pacientes_hint).html(html); 
+0

これは、検索が本当に速くなっていますが、iPhoneのブラウザでは十分速くはありませんが、とにかく感謝しています –

関連する問題