为输入字段创建自定义自动完成列表
我需要为输入字段创建建议的自定义自动完成列表。到目前为止,我已经将事件处理程序与html input元素的 input
事件相关联,以便在该事件触发时可以获取建议。
I need to create a custom auto-complete list of suggestions for an input field. So far I have associated an event handler to the input
event to the html input element, so that when that event triggers, I can fetch the suggestions.
问题是我将如何显示这些建议。默认情况下,输入元素可以显示建议,但是可以自定义/访问这些建议吗?
The problem is how would I show these suggestions. By default, input elements can display suggestions, but is it possible to customize/access those suggestions?
如果没有,那有什么选择?
If not, what would be the alternatives?
最好不要使用外部库。
您可以使用数据列表标记,但在IE< = 9
You may use 'datalist' tag but it doesn't work in IE <= 9
<!DOCTYPE html>
<html>
<body>
<form>
<input list="country" name="countru">
<datalist id="country">
<option value="U.S.">
<option value="France">
<option value="China">
<option value="Cambodia">
<option value="Chile">
<option value="Canada">
<option value="Poland">
</datalist>
<input type="submit">
</form>
</body>
</html>
运行代码并尝试键入'C ,然后是 a。
Run the code and try typing 'C' and then 'a'.