2012-06-14 45 views
2

我下載和編輯的代碼來自互聯網,基本上是我想要做的是創造我是從使用JSON數據庫讀取的字符串列表。 該列表是完美的,現在我想要做的是使用JQuery過濾結果(文本框)。 這樣做的最佳方式是什麼? 當用戶在文本框中鍵入內容時,顯示列表中的結果的完整列表。篩選名單上

感謝

$(function() 
{ 
    $.ajax({ 
     url: 'api.php', data: "", dataType: 'json', success: function(rows) 
     { 
      var list = $("#toggle").append('<ul></ul>').find('ul'); 
      for (var i in rows) 
      { 
      var row = rows[i]; 
       //var id = row[0]; 
       var Dname = row[4]; 
       Dname = Dname.toLowerCase(); 
       list.append("<li>"+Dname+"</li><div>Pulse</div>"); 
      } 
      $('ul li:odd').addClass('zebra_odd'); 
      $('ul li:even').addClass('zebra_even'); 
      $("li").click(function(){ 
       $(this).toggleClass("active"); 
       $(this).next("div").stop('true','true').slideToggle(); 
      }); 
     } 
    }); 
}); 
+1

[你嘗試過什麼?](http://whathaveyoutried.com/) – sczizzo

+0

你可以看一看[jQuery用戶界面自動完成(http://jqueryui.com/demos/autocomplete/)和[.grep()](http://api.jquery.com/jQuery.grep/) – Andy

回答

4

這個工作相當不錯:

$('input').keyup(function() {      // Bind keyup event to textbox 
    var textboxVal = $(this).val().toLowerCase(); // Get value of textbox 
    $('ul li').each(function() {     // loop through the list 
     var listVal = $(this).text().toLowerCase(); // get value of the <li> 
     if(listVal.indexOf(textboxVal) >= 0) {  // search if textboxVal is in listVal 
      $(this).show();       // if true show this <li> 
     } else { 
      $(this).hide();       // else hide this <li> 
     } 
    }); 
}); 

例如:http://jsfiddle.net/PWAXt/