从鼠标选择后未选择jQuery UI自动完成值

时间:2019-02-07 10:51:59

标签: javascript jquery autocomplete jquery-ui-autocomplete

我已经使用jquery创建了autocomplete的功能。效果很好,但例如:

每当我键入文本并使用鼠标从列表中选择值时,它都会被第一次选中,但是如果我再次键入并选择列表。不会选择这些值。

下面是我的代码。我不明白为什么它不起作用。

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

更新

下面的Vendor.ashx文件中是我使用的代码

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;
        }    
    }

1 个答案:

答案 0 :(得分:1)

您能否从请求中提供预期的JSON结果?

由于我没有JSON输出,因此我制作了一个基于AJAX的自动完成功能的示例。您应该输入3个字符以开始搜索。

$( "#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" />