【发布时间】:2012-09-27 08:08:55
【问题描述】:
请那些知道如何创建类似自动建议搜索框的 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>
代码似乎不起作用,甚至没有响应
请问有人知道如何以更有效的方式实现此功能
我真的需要这方面的帮助
【问题讨论】:
-
你可以看看一些不错的自动补全插件,例如jQuery UI's autocomplete。
-
你看过jquery自动完成吗? jqueryui.com/demos/autocomplete
标签: jquery twitter autocomplete ruby-on-rails-3.2 autosuggest