简体   繁体   English

jQuery的文本框自动完成不起作用

[英]jquery textbox autocomplete is not working

hello everyone I am new to jquery and i am at learning stage of jquery. 大家好,我是jquery新手,我正在学习jquery。 I am trying to create a auto complete textbox functionality using jquery.I am getting error here i dn't know how to get what error it is? 我正在尝试使用jquery创建自动完成的文本框功能。我在这里报错,我不知道如何得到什么错误? it just my code to error section of jquery code.. 它只是我的代码到jQuery代码的错误部分。

Here is my Jquery code 这是我的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>

and here is my c# code 这是我的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;
            }
        }

Please tell me why it is not working and how can i get what the exact error is 请告诉我为什么它不起作用以及如何获得确切的错误

Try this, this is helpfull for you. 试试这个,这对您有帮助。 there are two section 有两个部分

  1. Bind data to autocomplete element 将数据绑定到自动完成元素
  2. Fetch data from your code(that is BindSearch ) 从代码中获取数据(即BindSearch

I think this is easier for you easy to implement in your code 我认为这对您来说更容易在代码中轻松实现

Process:- After fetch data from code behind through BindSearch method data save in content variable and spilt trough '$' for easier to make it array, now copy all content variable data in item array and that item array response in autocomplete plugin which make item array in the form of ul-li for easy to design. 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);
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM