jquery文本框自动完成功能无效

时间:2013-09-14 06:36:51

标签: c# jquery asp.net web-services

大家好我是jquery的新手,我正在学习jquery的阶段。我正在尝试使用jquery创建一个自动完成文本框功能。我在这里得到错误我不知道如何得到它是什么错误?它只是我的代码到jquery代码的错误部分..

这是我的Jquery代码

<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        SearchText();
    });
    function SearchText() {
        $(".autosuggest").autocomplete({
            source: function (request, response) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "Home.aspx/GetData",
                    data: "{'Prefix':'" + document.getElementById('txtSearch').value + "'}",
                    dataType: "json",
                    success: function (data) {
                        response(data.d);
                    },
                    error: function (result) {
                        alert("Error");
                    }
                });
            }
        });
    }
</script>
</head>
<body>
    <form id="form1" runat="server">
        <div class="demo">
            <div class="ui-widget">
                <label for="tbAuto">Enter UserName: </label>
                <input type="text" id="txtSearch" class="autosuggest" />
            </div>

这是我的c#代码

[System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static List<string> GetData(string Prefix)
    {

        List<string> result = new List<string>();
        using (SqlConnection con = new SqlConnection("myconnectionstring"))
        {
            using (SqlCommand cmd = new SqlCommand("select hotelname from Hm_HotelMaster where hotelname=@hotelname+'%'", con))
            {
                con.Open();
                cmd.Parameters.AddWithValue("@hotelname", Prefix);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    result.Add(dr["hotelname"].ToString());
                }
                return result;
            }
        }

请告诉我它为什么不起作用,我怎样才能得到确切的错误

1 个答案:

答案 0 :(得分:0)

试试这个,这对你有帮助。有两个部分

  1. 将数据绑定到自动完成元素
  2. 从您的代码中获取数据(即BindSearch
  3. 我认为您可以更轻松地在代码中实现

    Process:-从后面的代码中获取数据后,通过BindSearch方法将数据保存在内容变量中,并通过'$'溢出以便于制作数组,现在复制项目数组中的所有内容变量数据和该项目数组响应自动完成插件,以ul-li的形式制作项目数组,便于设计。

    $("#txtSearch").autocomplete({
            minLength: 2,
            source: function (req, resp) {
                BindSearch('Home.aspx', 'GetData', "{Prefix:'" + $('#txtSearch').val() + "'}", function (response) {
                    content = response.split('$');
                    var item = new Array();
                    var i;
                    for (i = 0; i < (content.length - 1); i++) {
                        item[i] = content[i];
                    }
                    resp(item);
                });
            }
        });
    
    
    
    function BindSearch(U, F,D, callback) {
        $.ajax({
            type: "POST",
            url: U + '/' + F,
            data: D,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: true,
            cache: false,
            success: function (response) {
                callback(response.d);
            }
        });
    }