Bootstrap Typeahead only displaying first letter

情到浓时终转凉″ 提交于 2019-12-05 09:38:49

Check the combination of quotes that's output in your html for the data-source attribute. I was having the same problem with the following snippet of code.

<input type="text" name="test" id="test" class="span2" data-provide="typeahead" 
data-items="4" data-source="['thisone','another','zzzz']">

changing to the following, which I've seen in other examples, didn't fix it

data-source="["thisone","another","zzzz"]"

but switching to single quotes around the attribute value and double-quotes for the searchable elements fixed it. This works.

data-source='["thisone","another","zzzz"]'

For anyone who is getting this problem when using a source function (i.e. ajax call to your server):

use:

process(JSON.parse(data));

instead of:

process(data);

I just tried your code, and it worked for me. When I changed the quotes to be like this though, I was getting the issue you're having:

"['USA', 'Canada','Mexico']"

I was running into a similar problem, it's slightly different than your issue, but it might help others. What was happening to me is that the JSON I was passing to typeahead was being interpreted as a character array. So if I passed in:

["USA", "Canada","Mexico"]

It would interpret it as an array of 26 characters, thus the results typeahead was giving me were single characters.

In my case, it was because I was making an ajax call and I didn't set the dataType to 'json'. In your case that isn't the problem, but hopefully this gets you moving towards a solution!

var data = "";
$.ajax({
        url: 'ajax/getdirverandpass.php',
        success: function (result) {
            data = JSON.parse(result);
        },
        async: false
});

 $("#nav-search-input").typeahead({source: data, updater: function (a) {
            $("#nav-search-input").focus();
            return a
}});

please use this JSON.parse(result);

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!