1

我正在使用 jquery UI 自动完成

如果我在 jquery / asp.net 中这样做:

 $("#<%= txtName.ClientID %>").livequery(function () {
                $(this).autocomplete('LoadNames.ashx')
             .result(function (event, data, formatted) { // data[0] : Name, data[1] :AddressID
                 // Set Selected email ID to hidden field
                 $("#<%= hdnID.ClientID %>").val(data[1]);
                 if (data[1] != "0") {
                     var url = "http://emps/NewEntry.aspx?ID=" + data[1];
                     window.open(url);
                 }
                 else {
                 }
             });
            });

这很好用,但我想添加更多参数,如 minChars 等,所以我试试这个:

$("#<%= txtName.ClientID %>").livequery(function () {
                $(this).autocomplete({source:"LoadNames.ashx", delay:100, minChars:2})
             .result(function (event, data, formatted) { // data[0] : Name, data[1] :AddressID
                 // Set Selected email ID to hidden field
                 $("#<%= hdnID.ClientID %>").val(data[1]);
                 if (data[1] != "0") {
                     var url = "http://emps/NewEntry.aspx?ID=" + data[1];
                     window.open(url);
                 }
                 else {
                 }
             });
            });

然后这不再起作用。如何向此添加更多参数,例如 minChars、delay 等?

从有人想查看 .ashx 文件的评论中,就是这样,但这没有问题。它只是我想为自动完成添加额外的参数:

 public class LoadNames : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            DataSet ds = null;
            Dictionary<long, string> lstAddresses = new Dictionary<long, string>();

            ds = GetLoginNames();

            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    lstAddresses.Add(Convert.ToInt64(dr["LoginID"].ToString()), dr["Login"].ToString());
                }
                StringBuilder builder = new StringBuilder();

                foreach (KeyValuePair<long, string> item in lstAddresses)
                {
                    builder.Append(string.Format("{0}|{1}|{2}",
                          item.Value,
                          item.Key,
                          Environment.NewLine));
                }
                context.Response.Write(builder.ToString());
            }
        }

        public DataSet GetLoginNames()
        {
            SqlCommand cmdSelect = default(SqlCommand);
            SqlConnection conMyData = default(SqlConnection);
            SqlDataAdapter daIssues = default(SqlDataAdapter);
            System.Data.DataSet ds = null;

            conMyData = null;

            //try and make a connection   
            try
            {
                conMyData = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["connString"]);
                cmdSelect = new SqlCommand("selFullNames", conMyData);

                var _with1 = cmdSelect;
                _with1.CommandType = System.Data.CommandType.StoredProcedure;
                //add parameters
                _with1.Parameters.Add("@Inactive", SqlDbType.Int).Value = 2;
                daIssues = new SqlDataAdapter();
                daIssues.SelectCommand = cmdSelect;
                ds = new System.Data.DataSet();
                daIssues.Fill(ds);

                return ds;
                //catch any exceptions that might be thrown
            }
            catch (Exception e)
            {
                throw e;
                //clean up and close resources
            }
            finally
            {
                conMyData.Close();
                cmdSelect = null;
                conMyData = null;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
4

2 回答 2

0

知道了:

$(this).autocomplete("LoadNames.ashx", { minChars: 1, delay:100 })

于 2013-05-07T19:06:03.310 回答
0
$(document).on("keydown.autocomplete", "#<%= txtName.ClientID %>", function (e) {
    $(this).autocomplete({
        source: "LoadNames.ashx",
        delay: 100,
        minLength: 2
    }).result(function (event, data, formatted) {
        $("#<%= hdnID.ClientID %>").val(data[1]);
        if (data[1] != "0") {
            var url = "http://emps/NewEntry.aspx?ID=" + data[1];
            window.open(url);
        } else {}
    });
});
于 2013-05-02T20:19:38.627 回答