简体   繁体   English

自动完成文本字段

[英]Auto-complete text field

How do I make an autocomplete text field like the one here at Stack Overflow for the Tags fields? 如何在标签字段的Stack Overflow中创建一个自动完成文本字段? I want to use it for my Rails app. 我想将它用于我的Rails应用程序。

StackOverflow uses the jQuery Autocomplete Plugin , it has multiple item support. StackOverflow使用jQuery Autocomplete插件 ,它有多项支持。

Check the demos . 检查演示

Snippet from the edit page: 编辑页面中的代码段:

$("#tagnames").autocomplete("/tags/filter", {
    max: 6,
    highlightItem: true,
    multiple: true,
    multipleSeparator: " ",
    matchContains: true,
    scroll: true,
    scrollHeight: 300
});

You have 2 options you can do Rails Jquery UJS with the Autocomplete gem OR TokenInputs and look at the Railscasts on TokenFields on how to set it up for your situation. 你可以使用自动完成gemTokenInputs为 Rails Jquery UJS做2个选项,并查看TokenFields上的Railscasts如何根据你的情况进行设置。 Personally, i like what latter does with its own personal themes. 就个人而言,我喜欢后者用自己的个人主题做的事情。

The basic idea is, you trigger AJAX requests to the server, using onkeydown or onkeypress events (preferably with a bit in the way of delays), that do your search using the text entered so far and send back possible completions to the client. 基本的想法是,您使用onkeydown或onkeypress事件(最好有一点延迟)触发对服务器的AJAX请求,使用到目前为止输入的文本进行搜索并将可能的完成发送回客户端。 The client then does DOM manipulation to display these. 然后客户端执行DOM操作以显示这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM