简体   繁体   English

使用html5和javascript的下拉建议框

[英]dropdown suggestion box using html5 and javascript

I have googled thoroughly on this question, and most of the times ended up with the <datalist> tag. 我已经在这个问题上进行了详尽的搜索,大多数时候都以<datalist>标签结尾。 But in this case if the user commits a spelling mistake, the options will not be shown. 但是在这种情况下,如果用户犯了拼写错误,则不会显示选项。 So this is my question. 这就是我的问题。

I have an input search text box. 我有一个输入搜索文本框。

<input type = "text" name = "search" onkeyup = "someFunc()" />

In the javascript someFunc funtion, i make an ajax call and retrieve data and put the values in an array which I want to be displayed as suggestions. 在javascript someFunc函数中,我进行了ajax调用并检索数据,并将值放入要显示为建议的数组中。

<script>
function someFunc(){
//ajax call
suggestionArray = array(); // assume the array contains the values
}
</script>

Now the contents of this array have to be displayed as suggestions, like a dropdown from the input search box, and should be able to navigate using up down arrow keys. 现在,该数组的内容必须显示为建议,例如从输入搜索框的下拉列表,并且应该能够使用向上箭头键进行导航。 (say just like google search) (就像Google搜索一样)

Thanks in advance! 提前致谢!

EDIT 1:: 编辑1 ::

I want all the values in my array to be displayed as suggestions. 我希望将数组中的所有值显示为建议。

EDIT 2:: 编辑2 ::

The appropriate suggestions to be displayed is handled by my server side code, the server code returns an array of suggestions. 我的服务器端代码处理了要显示的适当建议,服务器代码返回了一组建议。 Now this array has to be displayed to the user. 现在必须将此数组显示给用户。 So if the keyword is always and the user typed aways , one of the possible values in the array will be always which has to be displayed as a suggestion. 因此,如果关键字always且用户键入了aways ,则将always显示数组中可能的值之一,该值必须显示为建议。

Try like this, Using jquery-ui plugin: 尝试这样,使用jquery-ui插件:

<input type='text' title='Tags' id='tags' />

$(document).ready(function() {

var aTags = ["ask","always", "all", "alright", "one", "foo", "blackberry",   "tweet","force9", "westerners", "sport"];

$( "#tags" ).autocomplete({
    source: aTags
});

});

check demo in pen: http://codepen.io/anon/pen/KVOBYb 查看笔式演示: http : //codepen.io/anon/pen/KVOBYb

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

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