简体   繁体   中英

jquery textbox autocomplete is not working

hello everyone I am new to jquery and i am at learning stage of jquery. I am trying to create a auto complete textbox functionality using jquery.I am getting error here i dn't know how to get what error it is? it just my code to error section of jquery code..

Here is my Jquery code

<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>
<script type="text/javascript">
    $(document).ready(function () {
        SearchText();
    });
    function SearchText() {
        $(".autosuggest").autocomplete({
            source: function (request, response) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "Home.aspx/GetData",
                    data: "{'Prefix':'" + document.getElementById('txtSearch').value + "'}",
                    dataType: "json",
                    success: function (data) {
                        response(data.d);
                    },
                    error: function (result) {
                        alert("Error");
                    }
                });
            }
        });
    }
</script>
</head>
<body>
    <form id="form1" runat="server">
        <div class="demo">
            <div class="ui-widget">
                <label for="tbAuto">Enter UserName: </label>
                <input type="text" id="txtSearch" class="autosuggest" />
            </div>

and here is my c# code

[System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static List<string> GetData(string Prefix)
    {

        List<string> result = new List<string>();
        using (SqlConnection con = new SqlConnection("myconnectionstring"))
        {
            using (SqlCommand cmd = new SqlCommand("select hotelname from Hm_HotelMaster where hotelname=@hotelname+'%'", con))
            {
                con.Open();
                cmd.Parameters.AddWithValue("@hotelname", Prefix);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    result.Add(dr["hotelname"].ToString());
                }
                return result;
            }
        }

Please tell me why it is not working and how can i get what the exact error is

Try this, this is helpfull for you. there are two section

  1. Bind data to autocomplete element
  2. Fetch data from your code(that is BindSearch )

I think this is easier for you easy to implement in your code

Process:- After fetch data from code behind through BindSearch method data save in content variable and spilt trough '$' for easier to make it array, now copy all content variable data in item array and that item array response in autocomplete plugin which make item array in the form of ul-li for easy to design.

$("#txtSearch").autocomplete({
        minLength: 2,
        source: function (req, resp) {
            BindSearch('Home.aspx', 'GetData', "{Prefix:'" + $('#txtSearch').val() + "'}", function (response) {
                content = response.split('$');
                var item = new Array();
                var i;
                for (i = 0; i < (content.length - 1); i++) {
                    item[i] = content[i];
                }
                resp(item);
            });
        }
    });



function BindSearch(U, F,D, callback) {
    $.ajax({
        type: "POST",
        url: U + '/' + F,
        data: D,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        cache: false,
        success: function (response) {
            callback(response.d);
        }
    });
}

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