开发者社区> 问答> 正文

如何使用js或者jquery实现如下图的自动补全效果

例如我输入nh2,然后会从系统查询和nh2相关的结果,然后自动补全,并且把自动输入的内容选中:如图:
screenshot

展开
收起
a123456678 2016-07-08 10:00:28 2013 0
1 条回答
写回答
取消 提交回答
  • <input id="i" type="text" value="One two three">
     
    <script type="text/javascript">
     
        function setInputSelection(input, startPos, endPos) {
            if (typeof input.selectionStart != "undefined") {
                input.selectionStart = startPos;
                input.selectionEnd = endPos;
            } else if (document.selection && document.selection.createRange) {
                // IE branch
                input.focus();
                input.select();
                var range = document.selection.createRange();
                range.collapse(true);
                range.moveEnd("character", endPos);
                range.moveStart("character", startPos);
                range.select();
            }
     
        }
     
        window.onload = function() {
            setInputSelection(document.getElementById("i"), 4, 7);
        };
     
    </script>
    2019-07-17 19:53:08
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
JavaScript异步编程 立即下载
Delivering Javascript to World 立即下载
编程语言如何演化-以JS的private为例 立即下载

相关实验场景

更多