Jquery autocomplete not showing the desired result

2019-09-06 12:16发布

问题:

I've this code

 $("#tb1").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "../mycontroller/getdata",
                dataType: "json",
                data: { strText: "" },
                success: function (Data) {
                    response($.map(Data.Data, function (item) {
                        return {
                            label: item.Name,
                            value: item.Id
                        };
                    }));
                }
            });
        },
        minLength: 1,
        select: function (event, ui) {
            alert(ui.item ? "Selected: " + ui.item.label : "Nothing selected, input was " + this.value);
        }
    });

I get the result in textbox, however it shows all the items in list instead of showing seleted items.

For example:- After i enter "ab" in textbox it should display all the items with ab but it shows items with ca, as and other alphabets combinations.

what am i doing wrong, how can i resolve it?

回答1:

Change this:

data: { strText: "" }

to:

data: { strText: $("#tb1").val() }

you are not sending the textbox entered value so it is bringing all records.