jQuery UI Autocomplete - get data from txt file

后端 未结 2 1607
半阙折子戏
半阙折子戏 2020-12-10 23:08

I\'m jQuery beginner trying to make jQuery UI Autocomplete get data from txt file. Txt format is simple:

user1
user2
user3
etc.

相关标签:
2条回答
  • 2020-12-10 23:45

    From the documentation:

    Source
    An Array of Strings:
    [ "Choice1", "Choice2" ]

    An Array of Objects with label and value properties:
    [ { label: "Choice1", value: "value1" }, ... ]

    You need to amend your text file to be in this format:

    ["user1","user2","user3"]
    

    Then change your jQuery to this:

    $( "#userLogin" ).autocomplete({
        source: 'users.txt'
    });
    
    0 讨论(0)
  • 2020-12-10 23:52

    The accepted solution did not work for me. This is how I read a .txt for my jQuery autocomplete input field:

    $.ajax({
        url: "foo/bar.txt",
        dataType: "text",
        success: function(data) {
            var autoCompleteData = data.split('\n');
            $("#input").autocomplete({
                source: function(request, response) {
                    var results = $.ui.autocomplete.filter(autoCompleteData, request.term);
                    response(results.slice(0, 10)); // Display the first 10 results
                }
            });
        }
    });
    
    0 讨论(0)
提交回复
热议问题