jQuery UI autocomplete values not getting selected after selection from mouse

Issue

I have created a functionality of autocomplete using jQuery. It works perfectly fine but for example:

Whenever I type a text and and select the values from the list using mouse, it gets selected for the first time but if I again type and select the list. The values don’t get selected.

Below is my code. Why is it not working?

$(document).ready(function () {
$('#txtAssignVendor').autocomplete({
    source: AppConfig.PrefixURL + 'VendorData.ashx',
    position: {
        my: "left bottom",
        at: "left top",
    }
});});

Update

And in Vendor.ashx file below is the code which I use

public void ProcessRequest(HttpContext context)
    {
        try
        {
            //DataTable dt = new DataTable();
            string term = context.Request["term"] ?? "";
            List<string> VendorNames = new List<string>();
            string connString = ConfigurationManager.ConnectionStrings["ConnAPP_NEIQC_PLNG"].ConnectionString;

            using (OracleConnection conn = new OracleConnection(connString))
            {
                OracleCommand cmd = new OracleCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = ConfigurationManager.AppSettings["PackageName"].ToString() + ".GET_VENDOR_NAME";
                cmd.Connection = conn;

                cmd.Parameters.Add(new OracleParameter { ParameterName = "P_VENDORNAME", Value = term, OracleDbType = OracleDbType.NVarchar2, Direction = ParameterDirection.Input });

                cmd.Parameters.Add(new OracleParameter
                {
                    ParameterName = "TBL_DATA",
                    OracleDbType = OracleDbType.RefCursor,
                    Direction = ParameterDirection.Output
                });

                if (conn.State != ConnectionState.Open) conn.Open();
                OracleDataAdapter da = new OracleDataAdapter(cmd);

               // da.Fill(dt);

                OracleDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    VendorNames.Add(dr["VENDORNAME"].ToString());
                    //VendorNames.Add(string.Format("{0}-{1}", dr["VENDOR_CODE"], dr["VENDOR_NAME"]));
                }
            }

            JavaScriptSerializer js = new JavaScriptSerializer();
            context.Response.Write(js.Serialize(VendorNames));
        }
        catch (Exception ex)
        {                
            throw;
        }    
    }

Solution

Could you provide the expected JSON result from the request?

AS I don’t have the JSON output, I have made a working example with an AJAX based autocomplete. You should type 3 characters to start the search.

$( "#country" ).autocomplete({
	source: function( request, response ) {
		$.ajax({
			type: 'GET',
			url: "https://restcountries.eu/rest/v2/name/" + request.term,
			dataType: "json",
			success: function( resp ) {
        		var data = []
        		resp.forEach(d => {data.push(d.name)})
	            response(data)
			}
		});
	},
	minLength: 3,
	select: function( event, ui ) {
		console.log( ui.item ?
			"Selected: " + ui.item.label :
			"Nothing selected, input was " + this.value);
	},
});
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<input type="text" id="country" />

Answered By – vmf91

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published