jQuery的自动完成功能不工作? [英] Autocomplete jquery not working?

查看:171
本文介绍了jQuery的自动完成功能不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a text box, Inside I want it to auto complete. The data for the auto complete is going to be given through the database.

这是我的jQuery的:

This is my Jquery:

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

这是我已经把autocompletetagdata现在:

This is what I have put in autocompletetagdata for now:

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']";     
    } 



我究竟做错了什么?

What am i doing wrong?

编辑:

 <script type="text/javascript" src="/scripts/js/jquery.scrollTo-min.js"></script>
<script type="text/javascript" src="/scripts/js/jquery.flash.min.js"></script>
<script type="text/javascript" src="/scripts/js/jquery.sifr.min.js"></script>
<script type="text/javascript" src="/scripts/js/global.js"></script>
<script type="text/javascript" src="/scripts/js/jquery-ui-1.8.17.custom.min.js"></script>
<script type="text/javascript" src="/scripts/js/orderstatus.js"></script>    
<script type="text/javascript" src="/scripts/js/jquery.ui.core.js"></script>
<script type="text/javascript" src="/scripts/js/jquery.ui.widget.js"></script>
<script type="text/javascript" src="/scripts/js/jquery.ui.datepicker.js"></script>
<script type="text/javascript" src="/scripts/js/jquery.qtip-1.0.0-rc3.min.js"></script>
<script type="text/javascript" src="/scripts/js/json_parse.js"></script>

当你走在你回来的屏幕上的浏览器autocompletetagdata..aspx ...

When you go to autocompletetagdata..aspx in the browser you get back on the screen...

SC052 SC053 SC055 SC060 SC061 SC062 SC063 SG011 SG014 SG015

萤火虫也确实显示正在发送回在响应这些项目,但没有任何反应到文本框

Firebug also does show these items being sent back in the response, but nothing happens to the text box

推荐答案

这是jQuery代码:

This is Jquery Code :

$("#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数据是这样的:

call you ajax request and return JSON data something like this :

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

有关收益的财产以后一样,你必须像这样定义一个类:

for return somthing like that you must define a class like this :

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



所以它只是足够的恢复此类对象的列表(列表<自动完成> )。因此,创建这个列表,并通过你的SqlCommand填充它,然后返回它作为您了XMLHttpRequest响应

so it just enough to return a list of this class objects (List<Autocomplete>).So create this list and fill it by your sqlcommand and then return it as response of your XMLHTTPRequest

我测试it.It的伟大工程的人

I tested it.It works great man

好luck.Foroughi

Good luck.Foroughi

这篇关于jQuery的自动完成功能不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆