简体   繁体   English

有没有办法使用纯 JS 使用数据列表中的顶部选项自动完成输入的值?

[英]Is there a way to autocomplete an input's value with the top placed option from the datalist using pure JS?

Is there a way to autocomplete an input value (while the user is typing) with the top placed option suggested from the datalist by pressing enter (and do that with pure JS)?有没有办法通过按回车键从数据列表中建议的顶部选项自动完成输入值(在用户键入时)(并使用纯 JS)?

Somehow all the information on the matter that I manage to find suggests only solutions using JQuery.不知何故,我设法找到的有关此事的所有信息仅建议使用 JQuery 的解决方案。 I'm quite a beginner using JavaScript, so I am not familiar with the library.我是一个使用 JavaScript 的初学者,所以我不熟悉这个库。 So, I wonder if there is any solution that can be perforemed using pure JS?所以,我想知道是否有任何可以使用纯 JS 执行的解决方案?

html does have the ability to support autocomplete, but it's very limited and leaves the options for autocomplete up to the browser. html 确实具有支持自动完成的能力,但它非常有限,并且将自动完成的选项留给了浏览器。 see https://www.w3schools.com/tags/att_input_autocomplete.asp .请参阅https://www.w3schools.com/tags/att_input_autocomplete.asp You can use a library to enhance a normal input box, or build your own.您可以使用库来增强普通输入框,或构建自己的输入框。 Usually the way they work is by putting a absolute positioned div under the input when it has keyboard focus.通常它们的工作方式是在输入具有键盘焦点时在输入下放置一个绝对定位的 div。 A library that does this that I'm familiar with is called jqueryui https://jqueryui.com/autocomplete/ .我熟悉的一个库称为jqueryui https://jqueryui.com/autocomplete/ this does use jquery, but there are other libraries out there that don't depend on such.这确实使用了 jquery,但是还有其他库不依赖于此。 https://tarekraafat.github.io/autoComplete.js/#/ comes up in a quick google search. https://tarekraafat.github.io/autoComplete.js/#/出现在谷歌快速搜索中。 you'll have to assess it's quality.你必须评估它的质量。

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

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