twitter bootstrap typeahead(未定义的方法'toLowerCase')

编程入门 行业动态 更新时间:2024-10-24 16:23:33
本文介绍了twitter bootstrap typeahead(未定义的方法'toLowerCase')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用twitter bootstrap从我的数据库中获取制造商。

I am trying to use twitter bootstrap to get the manufacturers from my DB.

因为twitter bootstrap typeahead不支持ajax调用我正在使用这个fork:

Because twitter bootstrap typeahead does not support ajax calls I am using this fork:

gist.github/1866577

在该页面中有评论,提到如何做我想做的事情。问题是当我运行我的代码时,我继续得到:

In that page there is this comment that mentions how to do exactly what I want to do. The problem is when I run my code I keep on getting:

未捕获的TypeError:无法调用未定义的方法'toLowerCase'

Uncaught TypeError: Cannot call method 'toLowerCase' of undefined

我用Google搜索并尝试将我的jquery文件更改为使用缩小版和非缩小版以及托管在Google代码上的文件,我不断获取同样的错误。

I googled around and came tried changing my jquery file to both using the minified and non minified as well as the one hosted on google code and I kept getting the same error.

我的代码目前如下:

$('#manufacturer').typeahead({ source: function(typeahead, query){ $.ajax({ url: window.location.origin+"/bows/get_manufacturers.json", type: "POST", data: "", dataType: "JSON", async: false, success: function(results){ var manufacturers = new Array; $.map(results.data.manufacturers, function(data, item){ var group; group = { manufacturer_id: data.Manufacturer.id, manufacturer: data.Manufacturer.manufacturer }; manufacturers.push(group); }); typeahead.process(manufacturers); } }); }, property: 'name', items:11, onselect: function (obj) { } });

在url字段我添加了

window.location.origin

window.location.origin

以避免在另一个问题上讨论的任何问题

to avoid any problems as already discussed on another question

在我使用 $。each()之前,我决定使用 $。map() 推荐Tomislav Markovski在类似问题

Also before I was using $.each() and then decided to use $.map() as recomended Tomislav Markovski in a similar question

任何人都知道为什么我一直会遇到这个问题?!

Anyone has any idea why I keep getting this problem?!

谢谢

推荐答案

Typeahead期望一个字符串列表作为源

Typeahead expect a list of string as source

$('#manufacturer').typeahead({ source : ["item 1", "item 2", "item 3", "item 4"] })

在您的情况下,您希望将其与对象列表一起使用。这样你就必须做一些改变才能使它有效

In your case you want to use it with a list of objects. This way you'll have to make some changes to make it works

这应该有效:

$('#manufacturer').typeahead({ source: function(typeahead, query){ $.ajax({ url: window.location.origin+"/bows/get_manufacturers.json", type: "POST", data: "", dataType: "JSON", async: false, success: function(results){ var manufacturers = new Array; $.map(results.data.manufacturers, function(data){ var group; group = { manufacturer_id: data.Manufacturer.id, manufacturer: data.Manufacturer.manufacturer, toString: function () { return JSON.stringify(this); }, toLowerCase: function () { return this.manufacturer.toLowerCase(); }, indexOf: function (string) { return String.prototype.indexOf.apply(this.manufacturer, arguments); }, replace: function (string) { return String.prototype.replace.apply(this.manufacturer, arguments); } }; manufacturers.push(group); }); typeahead.process(manufacturers); } }); }, property: 'manufacturer', items:11, onselect: function (obj) { var obj = JSON.parse(obj); // You still can use the manufacturer_id here console.log(obj.manufacturer_id); return obj.manufacturer; } });

更多推荐

twitter bootstrap typeahead(未定义的方法'toLowerCase')

本文发布于:2023-11-24 21:50:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626983.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:未定义   方法   bootstrap   twitter   typeahead

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!