Jquery AutoComplete firefox 中文 Ajax (option url or data) Jquery rails 自动完成

本文介绍如何在Rails应用中使用jQuery插件实现输入框自动补全功能,并针对中文输入进行了特别优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

application.html
[url]http://docs.jquery.com/Plugins/autocomplete[/url]
<!DOCTYPE html>
<html>
<head>
<title>AotoComplete</title>
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag 'AutoComplete/jquery','AutoComplete/jquery.autocomplete','AutoComplete/jquery.bgiframe.min' %>
<%= stylesheet_link_tag 'AutoComplete/jquery.autocomplete','AutoComplete/main' %>
<%= csrf_meta_tag %>
</head>
<body>

<%= yield %>

</body>
</html>


/view/products/_form.html
<script>
$(document).ready(function(){
$("#product_name").autocomplete('/products/search',{matchContains: false})}
</script>



products_controller.rb
....
def search
key_word = params[:q]
# Product.limit(params[:limit])
@product_names = Product.connection.select_values("select name from products where name like '%#{key_word}%'")
respond_to do |format|
format.html { render :text => @product_names.join("\n")}
end
end
...


routes.rb
  resources :products do
collection do
get 'search'
end
end


其实支持中文,网上也有教程。
在jquery.autocomplete.js的194行左右的
.bind("unautocomplete", function() {
select.unbind();
$input.unbind();
$(input.form).unbind(".autocomplete"); # 下面添加
}).bind("input", function() {
// @hack by liqt:support for inputing chinese characters in firefox
onChange(0, true);


希望对你有帮助, 我折腾好久。下面有这个autocomplete 的rails 3 demo


[b]推荐阅读[/b][url]http://compdocjos.blogspot.in/2010/11/creating-autocompleting-association.html[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值