简体   繁体   English

如何使用jquery marcopolo实现自动完成

[英]How to implement auto complete using jquery marcopolo

I'm trying to implement marcopolo, which is a jquery plugin to do auto complete. 我正在尝试实现marcopolo,这是一个自动完成的jquery插件。

Here are my two issues. 这是我的两个问题。

1) If I type a single letter or any word, all values for "Direction" will appear, which is not correct. 1)如果我输入单个字母或任何单词,将出现“方向”的所有值,这是不正确的。 I would like, for example, if I search for "Up", any words with those two letters will appear. 我想,例如,如果我搜索“向上”,则会出现带有这两个字母的任何单词。

2) If a user searches for "Up" and they hit enter or hit the submit button, they should be taken to the URL associated with that word. 2)如果用户搜索“Up”并且他们点击Enter或点击提交按钮,则应将他们带到与该词相关联的URL。

Github reference: https://github.com/jstayton/jquery-marcopolo Github参考: https//github.com/jstayton/jquery-marcopolo
Marcopolo example: http://jstayton.github.io/jquery-marcopolo/example1.html Marcopolo示例: http//jstayton.github.io/jquery-marcopolo/example1.html

And here's my codepen: http://codepen.io/anon/pen/rrwgwJ 这是我的代码: http ://codepen.io/anon/pen/rrwgwJ

HTML HTML

<input type="text" name="autoCom" id="autoCom"><input type="submit" class="send">

CSS CSS

ol {position: absolute; top: 20px;}
ol.mp_list {list-style-type: none;}

JS JS

$(function(){

$('#autoCom').marcoPolo({
        url: 'https://api.myjson.com/bins/ygdk',
        minChars: 1,
        submitOnEnter: true,
            formatData: function (data) {
                return data; //possible issue
            },
        formatItem: function (data, $item) {
            return data.Direction;
            },
            onSelect: function (data, $item) {
        //window.location = data.URL;
        this.val(data.Direction);
        window.open(data.URL,'_blank');
            }
    });

});

Thanks ahead for your help! 谢谢你的帮助!

问题是您正在发送带有<input/>元素内容的“q”变量,但是您配置为JSON响应的页面没有使用该“q”变量过滤任何内容,因此,您将始终获得同样的结果。

[{"Direction":"Up","URL":"http://www.google.com"},{"Direction":"Left","URL":"http://www.cnn.com"},{"Direction":"Right","URL":"http://www.engadget.com"},{"Direction":"Down","URL":"http://www.twitter.com"},{"Direction":"Upward","URL":"http://www.facebook.com"},{"Direction":"Leftwing","URL":"http://www.snapfish.com"},{"Direction":"Rightwing","URL":"http://www.gizmodo.com"},{"Direction":"Downward","URL":"http://www.fox.com"}]

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

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