2012-09-26 21 views
0

請那些知道如何創建一個Twitter或Facebook的自動建議搜索框?我一直在嘗試實現這一段時間...這裏是一些代碼,我從enter link description here得到。創建一個像自動建議在軌道搜索框的推特

<div class="search_index"> 


<div id = "testsearch"> 
<%=render :partial => 'search'%> // partial where the updated search elements will appear from 
</div> 

<%=form_tag(search_path,:method=>'get',:remote=>true) do%> 

    <%=text_field_tag :search%> 
    <%= submit_tag "search" ,:name=>nil%> 

<%end%> 

<%= set_focus_to_id 'search' %> // I have a helper "set_focus_to_id" 



<script> // small javascript file :) 

document.observe("dom:loaded", function() { // ensures the page is loaded first 
new Form.Element.Observer(     // Observes the text_field_tag every 2  seconds 
    'search', 
    2, 
    respondToChange       //refrences the function in the Layout  
    )           // on a change in search calls respondToChange 
    }); 

    </script> 

    </div> 

在標籤我有

<script> 
function respondToChange() { 
$('search').up('form').submit()   // The ".up finds the form in the DOM" 
}; 
</script> 

的代碼似乎並不奏效,它並不甚至迴應

請沒有任何一個知道如何在實現這個功能更有效的方式

我真的需要幫助這個

+1

你可以看看一些很好的自動完成插件,例如[jQuery UI的自動完成](http://jqueryui.com/demos/autocomplete/)。 –

+1

你看過jQuery的自動完成嗎? http://jqueryui.com/demos/autocomplete/ – Mutmatt

+0

這裏還有一個[這個jQuery插件的流行Rails集成](https://github.com/crowdint/rails3-jquery-autocomplete)。 –

回答