簡體   English   中英

相同的自動完成功能在一種解決方案中有效,但在另一種解決方案中無效

[英]The same autocomplete works in one solution, but not in the other one

我已經嘗試了一切,這讓我發瘋了。 我做了一個自動完成功能,可以在解決方案中使用,但是當我在主要解決方案中將其導出(復制並粘貼)時,它不再起作用,並且我不斷收到以下錯誤消息:“無法獲取未定義或null的屬性'length'參考”。 它不能是jquery版本和東西,因為它可以在其他解決方案中使用。

aspx:CodiceFiscale.aspx

<link rel="Stylesheet" href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/them/redmond/jquery-ui.css" />            
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.22/jquery-ui.js"></script>

<script type="text/javascript">
$(function () {
    $("#MainContent_provatxt").autocomplete({
        source: function (request, response) {
            var param = { cityName: $('#MainContent_provatxt').val() };
            $.ajax({
                url: "CodiceFiscale.aspx/GetCities",
                data: JSON.stringify(param),
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataFilter: function (data) { return data; },
                success: function (data) {
                    response($.map(data.d, function (item) {
                        return {
                            value: item
                        }
                    }))
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        },
        minLength: 3
    });
});

<asp:TextBox ID="provatxt" runat="server"></asp:TextBox> 

嗯,是的,我必須通過#MainContent_獲取元素,因為瀏覽器中文本框的ID與項目中的文本框的ID不同,但是在其他解決方案中效果很好。

背后的代碼:CodiceFiscale.aspx.cs

[WebMethod]
    public static List<string> GetCities(string cityName)
    {

        List<string> City = new List<string>();
        string query = string.Format("SELECT DISTINCT nome_comune FROM comuni WHERE nome_comune LIKE '%{0}%'", cityName);
        using (MySqlConnection conn = new MySqlConnection("server=localhost;Database=servizi; Uid=root; Pwd=root;"))
        {
            using (MySqlCommand cmd = new MySqlCommand(query, conn))
            {
                conn.Open();
                MySqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    City.Add(reader.GetString(0));
                }
            }
        }
        return City;
    }

您可以使用ClientIDMode控制客戶端ID,以使ID更可預測( 有關此內容 )。 服務器在您的success功能中返回什么?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM