[英]How to read json data in typeahead.js?
我想讀取JSON文件中的數據,然后將其與typeahead.js結合使用,這是一個鏈接
如果有人寫下任何單詞,例如EnglishWords [Best],它將顯示他/她列出所有以best或like best開頭的單詞,然后選擇該單詞,然后顯示該單詞的描述,但我不知道,因為我是新手在JavaScript和JSON非常感謝:)
這是我的json數據文件的示例
var Words = [
{"EnglishWords":"Best","Description":"an expression of good some this"},
{"EnglishWords":"Best wishes","Description":"an expression of hope"},
{"EnglishWords":"Application","Description":"Computing a program or piece of software"},
-
-
-
-
-
-
];
看來您有兩件事正在發生。 一種是自動完成功能,另一種是單詞/短語定義?
typeahead.js
只會解決第一部分,但似乎很容易。 改編自github頁面的示例,您將需要以下內容:
$('input.englishwords').typeahead({
name: 'words',
local: ['application', 'best', 'best wishes']
});
編輯:
您必須將JSON對象解析為數組或typeahead
輸入的對象。 就像是:
var typeList = [];
for (var i = 0; i < Words.length; i++) {
typeList.push(Words[i].EnglishWords);
}
then:
$('input.englishwords').typeahead({
name: 'words',
local: typeList
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.