如何完成自动完成jquery的后端

Beg*_*ner 0 asp.net jquery autocomplete

我有一个文本框,里面我希望它自动完成.自动完成的数据将通过数据库给出.

这是我的Jquery:

 var data = "autocompletetagdata.aspx"
    $("#item").autocomplete({
        source: data
    });


protected void Page_Load(object sender, EventArgs e) {
   return "['word', 'hello', 'work', 'oi', 'hey']";     
} 
Run Code Online (Sandbox Code Playgroud)

小智 5

试试这个:

$("#item").autocomplete({
    source: function (request, response) {
        $.ajax({
            type: "POST",
            url: "autocompletetagdata.aspx/Search",
            data: { "search": request.term },
            contentType: "application/json; charset=utf-8",
            success: function (results) {
                var data = $.parseJSON(results);
                response($.map(data, function (item) { 
                    return { value: item } 
                }))
            }
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

在您的代码中,将其设为web方法:

[WebMethod]
public static void Search(string search)
{
    string[] list = new [] { "word", "hello", "work", "oi", "hey" };

    return list.Where(x => x.StartsWith(search));
}
Run Code Online (Sandbox Code Playgroud)