简体   繁体   中英

Ajax autocomplete not working with json

Does anyone know why this doesn't work? The server is returning JSON properly as far as I can tell. Example here: http://www.mediaworks.cc/test/sandbox2.aspx

The left textbox is the autocomplete. The right textbox populates when an item is clicked on in the autocomplete.

Here is the code:

<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
    <title></title>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>

    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $(".autosuggest").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "Sandbox2.aspx/GetPackages",
                        data: "{'term':'" + document.getElementById('txtPackage').value + "'}",
                        dataType: "json",
                        success: function (data) {
                            response($.map(data, function (item) {
                                return {
                                    label: item.HotelPackage,
                                    value: item.ID
                                };
                            }));
                        }
                    });
                },
                select: function (event, ui) {
                    document.getElementById('selectedValue').value = ui.item.HotelPackage;
                }
            });
        })
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:TextBox ID="txtPackage" runat="server" CssClass="autosuggest"></asp:TextBox>
        <asp:TextBox ID="selectedValue" runat="server"></asp:TextBox>
    </form>
</body>
</html>

Your id and class are referring the same element.

 <asp:TextBox ID="txtPackage" runat="server" CssClass="autosuggest">

assigning autocomplete to .autosuggest textbox

$(".autosuggest").autocomplete({  

And the data part is also referring the same element

data: "{'term':'" + document.getElementById('txtPackage').value + "'}",

Both are same, which is wrong.

Make sure the data part is right

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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