在我写名字的搜索框中,它应该询问文本文件中的建议。 例如:如果用户写了" Aka"然后代码应检查包含名为" Aka"的字符串的单词。并给出建议。 如果我将数据放入脚本本身,我可以这样做,如下所示:
$( function() {
var avail = [
"abc",
"akarsh",
"ads"
];
$( "#tags" ).autocomplete({
source: avail
});
} );
但我需要使用文件来做..我无法做到..
$( function() {
var availableTags = load('detail.txt');
$( "#tags" ).autocomplete({
source: availableTags
});
} );
HTML:
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags">
</div>
答案 0 :(得分:0)
你可以使用select2插件,默认返回JSON格式是(id,text),例如你的detail.txt文件是:
[ {"id": 1, "text": "abc"},
{"id": 2, "text": "akarsh"},
{"id": 3, "text": "ads"} ]
并从文件中加载JSON,如bellow:
$('#tags').select2({
ajax: {
dataType: "json",
url: "detail.txt",
results: function (data) {
return {results: data};
}
}
});