【问题标题】:jquery autocomplete plugin dose not work in masterpagejquery自动完成插件在母版页中不起作用
【发布时间】:2012-12-07 19:22:04
【问题描述】:

我一直在开发一个汽车零部件网站,我需要在搜索框中提供自动完成功能。

我已经尝试过的事情:

i)[我尝试使用服务使用 ajax 扩展器,但运气不佳] ii)[我也尝试过 jquery ui 内置自动完成插件,没有运气]

但是经过一天的全力奋斗,我终于在简单的 aspx 页面上运行了它,但是当我将此代码用于母版页时,它就不起作用了。

在这部分我需要你们 :)

//code inside the masterpage head
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
<script src="scripts/jquery.autocomplete.min.js" type="text/javascript"></script>
<link href="css/jquery.autocomplete.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
    $(document).ready(function() {
    $("#ctl00_myTextBox").autocomplete('select.ashx');
    });
 </script>

//注意:此文本框也在母版页中,而不是在任何内容页中

<asp:TextBox ID="myTextBox" runat="server" Width="250" ></asp:TextBox>

//这是httphandler的代码... //注意:我正在使用 LuceneIndexes 检索数据

公共类选择:IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/javascript";
        if (!String.IsNullOrEmpty(context.Request.QueryString["q"]))
       {
           foreach (string s in GetAutoCompleteValues(context.Request.QueryString["q"]))
           {
               context.Response.Write(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(s)+Environment.NewLine);
           }
       }

    }
    public static string[] GetAutoCompleteValues(string prefixText)
    {
           DataTable dt = GetSearchList.GetResult(prefixText);
           List<string> RowNames = new List<string>();
           foreach (DataRow drow in dt.Rows)
           {
               RowNames.Add(drow[1].ToString() + " " + drow[2].ToString() + " " + drow[3].ToString() + " " + drow[4].ToString() + " " + drow[5].ToString() + " " + drow[6]);
           }
           return RowNames.ToArray();
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

请提前帮助我任何建议都会有所帮助...

【问题讨论】:

    标签: jquery asp.net autocomplete master-pages


    【解决方案1】:

    这个问题是因为你没有使用动态渲染的控件id,从你放到母版页的那一刻起,id就变了。

    在您的 javascript 的这一行中,使用 myTextBox.ClientID 作为:

    $("#<%=myTextBox.ClientID%>").autocomplete('select.ashx');
    

    动态渲染控件id,让jQuery找到它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-29
      • 2012-10-05
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2015-04-18
      • 1970-01-01
      相关资源
      最近更新 更多