简体   繁体   中英

autocomplete is not working help is required

I have applied this upon an input type text, but it is not working. I need some guidance to show me where I am going wrong. This is javascript code and is not working:

$(document).ready(function () {
    $('#reasondescriptiontxtbox').autocomplete( {
        source: function (request, response) {
            $.ajax( {
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Default.aspx/getReason",
                data: "{'keywords':'" + request.term + "'}",
                dataType: "json",
                async: true,
                success: function (data) {
                    response(data.d);
                },
                error: function (result) {
                    //alert("Error");
                }
            });
        },
        minLength: 2
    });
});

code behind:

[WebMethod]
public static IList<string> getReason(string keywords)
{
    int count = 0;
    IList<string> result = new List<string>();
    string constr 
        = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
    SqlConnection con1 = new SqlConnection(constr);
    SqlCommand cmd1 = con1.CreateCommand();
    cmd1.CommandText = "select distinct Code_Description from CODE_DESCRIPTION_277 where Code_Description '%" + keywords + "%'";

    try
    {
        con1.Open();
        SqlDataReader dr = cmd1.ExecuteReader();

        while (dr.Read())
        {
            count++;
            result.Add(dr["Code_Description"].ToString());

            if (count == 100)
                break;
        }

        con1.Close();

        return result;
    }
    catch
    {
        return null;
    }
}

Will I have to add some sort of jQuery file?

Your SQL code appears to have a missing like in where Code_Description '%" + keywords + "%'" , could that be the cause?

You'd get no results and probably an SQL exception that is being masked by your Catch.

Try changing that line to

cmd1.CommandText = "select distinct Code_Description from CODE_DESCRIPTION_277 where Code_Description like '%" + keywords + "%'";

If you are using Master page then your javascript code should be like this $('#<%= textbox1.ClientID %>').autocomplete({

and make sure that you have included any version of jquery.js

I would organize unit tests into your project so you can test getReason in isolation, with mocked input. Then you can divide and conquer to determine where the problem is. If the issue is on the client side, you'll need to attach a debugger to the browser and set a breakpoint in your handler(s) to inspect locals and flow of execution.

我添加了类似关键字的关键字,这也是一个问题,其次,我将其更改为: $('#reasondescriptiontxtbox').autocomplete(更改为: $('input[id$=reasondescriptiontxtbox]').autocomplete(现在可以正常使用了

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