【问题标题】:JSONP Freebase for autocomplete jquery plugin用于自动完成 jquery 插件的 JSONP Freebase
【发布时间】:2011-09-15 06:29:00
【问题描述】:

来自Parse JSON Freebase results in PHP 但由于它可以使用 JSONP 在 Javascript 中完成,我想知道如何。

所以,我正在使用这个 jquery 自动完成插件:http://devthought.com/wp-content/projects/jquery/textboxlist/Demo/

这是在输入上使用插件的代码:

$(function() {
    var t = new $.TextboxList('#form_topick_tags', {unique: true, plugins: {autocomplete: {
            minLength: 2,
            queryRemote: true,
            remote: {url: 'autocomplete2.php'}
        }}});

我想解析来自 Freebase 的结果,例如http://www.freebase.com/private/suggest?prefix=beatles&type_strict=any&category=object&all_types=false&start=0&limit=10&callback=

并按此顺序将其传递给插件:

guid,"name",null,"name<span>n:type name</span>"

所以,第一个结果是:

0,"The Beatles",null,"The Beatles<span>Band</span>"

【问题讨论】:

  • 这是一个不错的计划!所以..你有什么问题?

标签: jquery autocomplete jsonp freebase


【解决方案1】:
<input id="form_topick_tags" />

<!-- Adjust the script tag locations per your set-up -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="GrowingInput.js" type="text/javascript" charset="utf-8"></script>         
<script src="../Source/TextboxList.js" type="text/javascript" charset="utf-8"></script>     
<script src="../Source/TextboxList.Autocomplete.js" type="text/javascript" charset="utf-8"></script> 
<!-- required for TextboxList.Autocomplete if method set to 'binary' --> 
<script src="../Source/TextboxList.Autocomplete.Binary.js" type="text/javascript" charset="utf-8"></script>     


<script type="text/javascript">
var search = 'beatles',
    myJSONArray = [];

$(function() {
    var t = new $.TextboxList('#form_topick_tags', {
        unique: true, 
        plugins: {
            autocomplete: {
                minLength: 2
            }
        }
    });

    $.ajax({
        dataType:'JSONP',
        success: function (obj) {
            for (var i=0, orl=obj.result.length; i < orl; i++) {
                var o = obj.result[i];
                myJSONArray.push([o.guid, o.name, o.name+'<span>'+o['n:type'].name+'</span>']);
            }

            // For testing:

            // alert(myJSONArray);
            // You can just use myJSONArray, but if you need JSON, see http://json.org for a JSON converter; in modern browsers, JSON is supported by default
            //alert(JSON.stringify(myJSONArray)); 

            t.plugins['autocomplete'].setValues(myJSONArray);
        }, 
        url: 
        'http://www.freebase.com/private/suggest?type_strict=any&category=object&all_types=false&start=0&limit=10&prefix='+encodeURIComponent(search)
    });
});
</script>

【讨论】:

  • 我必须将“搜索”变量声明为“披头士”吗?因为“搜索”必须是输入字段上的文本...
  • 不,你没有。只需替换要搜索的输入字段的值,例如 var search = document.getElementById('mysearchElement').value, 并根据按钮单击事件运行整个代码,而不是在页面就绪时运行。
猜你喜欢
  • 1970-01-01
  • 2012-04-26
  • 1970-01-01
  • 2011-02-13
  • 2011-06-06
  • 1970-01-01
  • 2015-12-13
  • 1970-01-01
  • 2012-01-29
相关资源
最近更新 更多