自动完成jquery无法正常工作?

我有一个文本框,里面我希望它自动完成。 自动完成的数据将通过数据库给出。

这是我的Jquery:

var data = "autocompletetagdata.aspx" $("#item").autocomplete({ source: data }); 

这就是我现在放在autocompletetagdata中的内容:

 protected void Page_Load(object sender, EventArgs e) { string term = Request.QueryString["term"]; SqlConnection myConnection = new SqlConnection(connStr); myConnection.Open(); string SQL = ("select Top 10 LTRIM(RTRIM(PGPRDC)) As PGPRDC FROM SROPRG SROPRG"); SqlCommand myCommand = new SqlCommand(SQL, myConnection); StringBuilder sb = new StringBuilder(); try { SqlDataReader reader = myCommand.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { sb.Append(reader.GetString(0)) .Append(Environment.NewLine); } } reader.Close(); } catch (Exception ex) { myConnection.Close(); } myConnection.Close(); Response.Write(sb.ToString()); //return "['word', 'hello', 'work', 'oi', 'hey']"; } 

我究竟做错了什么?

编辑:

             

当你在浏览器中转到autocompletetagdata..aspx时,你会回到屏幕上…

 SC052 SC053 SC055 SC060 SC061 SC062 SC063 SG011 SG014 SG015 

Firebug也确实显示这些项目在响应中被发回,但文本框中没有任何反应

这是Jquery代码:

 $("#txt1").autocomplete({ source: function (request, response){ $.ajax({ type: "POST", url: "YourAddress", contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { response($.map(data.d, function (item) { return { id: item.Value, value: item.Text } })) } }); }, select: function (event, ui) { $("#hdnId").val(ui.item.id);//Put Id in a hidden field } }); 

叫你ajax请求并返回这样的JSON数据:

 [{"Value":val1,"Text":"text1"}, {"Value":val2,"Text":"text2"}] 

对于像这样的返回,你必须定义一个这样的类:

 public class Autocomplete { private int val; private string text; public int Value { get { return val; } set { val = value; } } public string Text { get { return text; } set { text = value; } } } 

所以它就足以返回这个类对象的ListList )。所以创建这个列表并用sqlcommand填充它,然后将它作为XMLHTTPRequest的响应返回

我测试了它。它很棒

祝你好运。Foroughi

好吧,我不确定这是否像我想的那样,但是: autocompletetagdata.aspx可能正在显示数据,但是当你做源:数据时 ,数据没有准备好,我的意思是它不包含数据。 我通过在回调函数中填充来解决类似的问题(这里是autocompletetagdata.aspx的回调)。

看起来你的ASPX脚本从数据库发送一个新行分隔的值列表。 自动完成小部件实际上需要JSON编码的字符串或对象数组。 您可以使用JsonTextWriter类来创建JSON编码数据。 示例代码:

 SqlDataReader reader = myCommand.ExecuteReader(); JsonTextWriter writer = JsonTextWriter(Response.Output); writer.WriteStartArray(); // we must send a "[" even if there is no data if (reader.HasRows) { while (reader.Read()) { writer.WriteString(reader.GetString(0)); } } writer.WriteEndArray(); // we must send a "]" even if there is no data reader.Close(); 

我有自动完成问题。 我到达这里因为你包括qtip。 这两个库(jquery / autocomplete和qtip)在使用时都有问题。