代码实现:
<p><%= label_tag "principal_search",l(:label_principal_search) %><%= text_field_tag 'principal_search',nil %></p>
<%= observe_field(:principal_search,
:frequency => 0.5,
:update => :principals,
:url => { :controller => 'members',:action => 'autocomplete_for_member',:id => @project },
:with => 'q')
%>
<div id="principals">
</div>
observe_field的作用为每隔0.5秒查看id为principal_search的作用域,若值发生变化,请求url,并将返回的结果呈现在id为principals的模块上;
参数说明:
:principal_search 被监听的id
:frequency => 0.5 每0.5秒执行一次
:update => :principals 结果呈现在id为principals对应的模块上
:url => { :controller => 'members',:id => @project } 请求的url
:with => 'q‘ ajax请求传递的参数为“q=” +principal_search 原文链接:https://www.f2er.com/ajax/163458.html