我尝试遵循从plunker中挑选的代码来减少对数据库的ajax请求。 JSON 格式也可以按照文本文件中的示例生成良好。
但是当我尝试填充自动完成选项时,它只显示开头的一个字符。但是当我直接将 json 输出与 items 变量一起使用时,它就可以正常工作。
笨蛋 Plunker Link
Keywords.txt 文件中的 JSON 示例
["Mis","Operation","Operation Manager","Php","Sales","Telecalling","Analytics","Ceo","Commercials"];
代码
$(function()
{
var items = 'Keywords.txt';
function split( val )
{
return val.split( /,\s*/ );
}
function extractLast( term )
{
return split( term ).pop();
}
$( "#keyword" )
.autocomplete(
{
minLength: 1,
source: function( request, response )
{
response( $.ui.autocomplete.filter(items, extractLast( request.term ) ) );
},
focus: function()
{
return false;
},
select: function( event, ui )
{
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
});
最佳答案
我认为你的问题是你有一个字符串,尝试解析响应:
$.get('Keywords.txt').then(function(keywords){
items = JSON.parse(keywords);
});
关于javascript - 本地文本文件中的 jquery 自动完成功能未获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36872650/