简体   繁体   English

从本地文本文件无法自动提取jQuery

[英]jquery autocomplete from local text file not picking up

I have tried following code picked from plunker to reduce ajax request to database. 我尝试了下面的代码从plunker中选择,以减少对数据库的ajax请求。 JSON format is also generated fine as per example in text file. 按照示例,在文本文件中还可以很好地生成JSON格式。

But when i'am trying to populate autocomplete options it's showing only one character of beginning. 但是,当我尝试填充自动完成选项时,它仅显示开始的一个字符。 But when i use json output directly with items variable then it works fine. 但是,当我直接将json输出与items变量一起使用时,它可以正常工作。

Plunker Plunker Link 柱塞 柱塞链接

JSON example in Keywords.txt file keyword.txt文件中的JSON示例

["Mis","Operation","Operation Manager","Php","Sales","Telecalling","Analytics","Ceo","Commercials"];

Code

$(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;
        }
    });
});

I think that your problem is that you have a string, try to parse the response: 我认为您的问题是您有一个字符串,请尝试解析响应:

$.get('Keywords.txt').then(function(keywords){
    items = JSON.parse(keywords);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM