Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
504 views
in Technique[技术] by (71.8m points)

javascript - typeahead.js-未捕获的TypeError:this.displayText(…).toLowerCase不是函数(typeahead.js - Uncaught TypeError: this.displayText(…).toLowerCase is not a function)

I've been messing around with typeahead.js templates.(我一直在搞乱typeahead.js模板。)

No matter what I do, I keep getting the following error:(无论我做什么,都会不断出现以下错误:) Uncaught TypeError: this.displayText(...).toLowerCase is not a function in typeahead.min.js:2(未捕获的TypeError: this.displayText(...)。toLowerCase不是typeahead.min.js:2中的函数) My JavaScript looks as following:(我的JavaScript如下所示:) $('#search-field').typeahead({ display: 'value', templates: { suggestion: function(data) { return '<p><strong>' + data.value + '</strong> – ' + data.titleid + '</p>'; } }, source: function (query, process) { return $.get(root + '/api/search.php', { query: query }, function (data) { console.log('data:' + data); //outputs the JSON correctly. data = $.parseJSON(data); return process(data); }); } }); Where /api/search.php looks like:(/api/search.php如下所示:) [{"value":"Name1","titleid":"Test1"},{"value":"Name2","titleid":"Test2"},{"value":"Name3","titleid":"Test3"}] I've tried copying code from other examples directly into mine, sometimes even the whole piece, but that didn't solve it either, even though it did work in the example.(我曾尝试将其他示例中的代码直接复制到我的代码中,有时甚至复制到我的整个代码中,但这也没有解决它,即使它在示例中确实起作用。) How can I solve this, and get the template to work?(如何解决此问题,并使模板正常工作?)   ask by Appel Flap translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can look at this issue and this issue on GitHub.(您可以在GitHub上查看此问题此问题 。)

It appears that your objects in the source must have a name property, or you must define a displayText function yourself.(看来, source中的对象必须具有name属性,或者您必须自己定义displayText函数。) The second issue linked has a response which states:(链接的第二个问题有一个回应,指出:) The document should make clear that: "You can add all the properties you wish on your objects, as long as you provide a "name" attribute OR you provide your own displayText method."(该文档应明确指出:“只要提供“名称”属性,或者提供自己的displayText方法,就可以在对象上添加所有希望的属性。) And the solution is to add a displayText function to your typeahead options, like this:(解决方案是将displayText函数添加到您的typeahead选项中,如下所示:) $('#search-field').typeahead({ ... displayText: function (item) { return item.value; } ... }); Where item.value can be whatever property you wish to display.(其中item.value可以是您希望显示的任何属性。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...