简体   繁体   English

Twitter引导程序-提前输入

[英]Twitter bootstrap - typeahead

i use Twitter Bootstrap and typeahead. 我使用Twitter Bootstrap并提前输入。

i download the plugin: Twitter Bootstrap Typeahead Plugin Extension 我下载了插件:Twitter Bootstrap Typeahead插件扩展

and so long so good, its working when i use a static javascript json array eg. 而且这么长时间如此好,当我使用静态javascript json数组时,它的工作原理。

$('#demo1').typeahead({
        source: [
            { id: 9000, name: 'Aalborg' },
            { id: 2, name: 'Montreal' },
            { id: 3, name: 'New York' },
            { id: 4, name: 'Buffalo' },
            { id: 5, name: 'Boston' },
            { id: 6, name: 'Columbus' },
            { id: 7, name: 'Dallas' },
            { id: 8, name: 'Vancouver' },
            { id: 9, name: 'Seattle' },
            { id: 10, name: 'Los Angeles' }
        ],
        itemSelected: displayResult
    });

When i try to use ajax its will do enerything, my code look like this. 当我尝试使用ajax时,它会做大量工作,我的代码如下所示。

$('.typeahead').typeahead({
            ajax: '/actions/search/synonymSearch',
            itemSelected: displayResult
        });

and its return this json array ( i can rebuild its on all ways, and i can't get it to work ) 并且它返回这个json数组(我可以在所有方面重建它,而我无法使其工作)

[
    { id: 1, name: 'Toronto' },
    { id: 2, name: 'Montreal' },
    { id: 3, name: 'New York' },
    { id: 4, name: 'Buffalo' },
    { id: 5, name: 'Boston' },
    { id: 6, name: 'Columbus' },
    { id: 7, name: 'Dallas' },
    { id: 8, name: 'Vancouver' },
    { id: 9, name: 'Seattle' },
    { id: 10, name: 'Los Angeles' }
]

The plugin home pages. 插件主页。 https://github.com/tcrosen/twitter-bootstrap-typeahead https://github.com/tcrosen/twitter-bootstrap-typeahead

i hobe enybardy can help me here :) 我希望enybardy可以在这里帮助我:)

thanks a lot for all helping, :) 非常感谢大家的帮助,:)

EDIT - Problem resovle! 编辑-问题解决! :) Just need to added header("content-type: application/json"); :)只需要添加header("content-type: application/json"); into my PHP file, hobe this answere are usefull for orther peopole! 到我的PHP文件中,这个答案对其他极有用的人! :) :)

I don't think typeahead can read the source of your array and therefore I would parse it first. 我不认为typeahead可以读取您的数组source ,因此我将首先对其进行解析。

var data = [{"id":"9000","name":"Aalborg"},{"id":"9220","name":null},{"id":"9210","name":null},{"id":"9200","name":"Aalborg SV"}];

// using underscore.js get an array of the city names
var cities = _.pluck(data, "name");

// now start typeahead
$el.typeahead(
    source: cities,
    // the value of the `city` selected gets passed to the onselect  
    onselect: function(city) {
         // get the index of city selected from the data array
         var i = _.indexOf(data, city);
         // then using the index get what ever other value you need from the array
         // and insert in the DOM etc..

    }
);

This problem you are experiencing may be due to the page that you are calling via ajax isn't returning the right headers. 您遇到的此问题可能是由于您通过ajax调用的页面未返回正确的标题而引起的。

If you are using PHP try adding header('Content-type: application/json'); 如果您使用的是PHP,请尝试添加header('Content-type: application/json'); to the document that contains the JSON array. 到包含JSON数组的文档。

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

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