简体   繁体   English

jQueryUI自动完成插件不起作用

[英]jQueryUI Autocomplete Plugin not working

I want to implement something like autocomplete feature like this: 我想实现类似自动完成功能的功能:

http://jqueryui.com/demos/autocomplete/ http://jqueryui.com/demos/autocomplete/

However I cannot do that. 但是我不能那样做。 Here is my code: 这是我的代码:

    <script src="../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script src="../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="../Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script src="../Scripts/jquery.autocomplete.js" type="text/javascript"></script>
    <link href="../Styles/jquery.autocomplete.css" rel="stylesheet" type="text/css" />
    <script src="../Scripts/JScript-1.5.1.js" type="text/javascript"></script>
    <script src="../Scripts/jQueryUI.js" type="text/javascript"></script>

<script type ="text/javascript">
 $(function () {
     var availableTags = [
        "ActionScript",
        "AppleScript",
        "Asp",
        "BASIC",
        "C",
        "C++",
        "Clojure",
        "COBOL",
        "ColdFusion",
        "Erlang",
        "Fortran",
        "Groovy",
        "Haskell",
        "Java",
        "JavaScript",
        "Lisp",
        "Perl",
        "PHP",
        "Python",
        "Ruby",
        "Scala",
        "Scheme"
    ];
     $('[id$="tbSearch"]').autocomplete({
         source: availableTags
     });
 });

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

Nothing is showing as a dropdown in my textbox: tbSearch. 我的文本框中没有显示为下拉列表:tbSearch。 What is going wrong here? 这是怎么了?

Two problems: 两个问题:

  1. Your code comes before the HTML it wants to affect. 您的代码位于要影响的HTML 之前
  2. Even if you fix (1), you'll have the problem that the actual client-side "id" value will not be the value you coded into your ASP tag. 即使您修复(1),也将遇到一个问题,即客户端的实际 “ id”值将不是您编码到ASP标记中的值。 You have to call some ASP thing to get the client-side value. 您必须调用一些ASP东西才能获得客户端值。

oh wait: also, it's ".click", not ".Click". 哦,等等:也是“ .click”,而不是“ .Click”。

It would also be an extremely good idea to use the var keyword where you declare/define the "data" variable in the "success" handler. 在“成功”处理程序中声明/定义“数据”变量的地方使用var关键字也是一个非常好的主意。

edit — notice that in the example to which your question linked, the jQuery setup code is inside a jQuery "ready" handler. 编辑 -请注意,在您的问题所链接到的示例中,jQuery设置代码位于jQuery“就绪”处理程序内。 That will ensure that problem 1 is fixed. 这样可以确保解决问题1。 I'm not sure why you're wiring a "click" handler up to a "text box" element; 我不确定为什么要将“点击”处理程序连接到“文本框”元素。 if that generates an <input type="text"> tag then you won't get any clicks from that anyway (I don't think) ( edit that's wrong); 如果生成一个<input type="text">标记,那么无论如何您都不会获得任何点击(我不认为)( 编辑是错误的); even if you did, it's kind-of weird. 即使您这样做,也有点奇怪。

One issue I see here is that you're referencing ID "tbSearch" as follows: 我在这里看到的一个问题是,您引用的ID为“ tbSearch”,如下所示:

$("#tbSearch").autocomplete(data);

Yet once the .NET framework renders this page, the client ID of the textbox will be much different. 但是,一旦.NET框架呈现了此页面,文本框的客户端ID将大为不同。 You can use the following instead to make sure the ID you're referencing in your jQuery code is correct: 您可以改用以下代码来确保您在jQuery代码中引用的ID是正确的:

$('#<%= tbSearch.ClientID %>').autocomplete(data);

A couple of things you can do to debug right off the bat: 您可以立即进行以下几项调试:

  1. Add an error handler to your jquery .ajax call. 将错误处理程序添加到您的jquery .ajax调用中。

      error: function (xmlHttpRequest, textStatus, errorThrown) { alert(errorThrown); } 
  2. Set a breakpoint in your web method. 在Web方法中设置一个断点。 Are you hitting it? 你在打吗?

As Keith pointed out, you are overwriting the response that you are getting from the server: 正如Keith指出的那样,您将覆盖从服务器获得的响应:

success: function (data) {                    
   data = msg.d.split(" ");             
   $('#<%= tbSearch.ClientID %>').autocomplete(data);                 
} 

Perhaps what you are trying to do is: 也许您正在尝试做的是:

data = data.split(" ");

When you click your text box, and the Click() event is fired, all you are doing is providing the Data for the autocomplete. 当您单击文本框并触发Click()事件时,您要做的只是为自动完成功能提供数据。 You aren't actually invoking the drop down. 您实际上并不是在调用下拉菜单。 To do that, you need to call the search method. 为此,您需要调用搜索方法。 Also, keep in mind that there is a minLength property which can prevent the drop-down from opening: 另外,请记住,有一个minLength属性可以防止下拉列表打开:

//Set the minLength to zero
$("#tbSearch").autocomplete("minLength", 0");
//Now invoke the search   
$("#tbSearch").autocomplete("search", "");

In your ajax request you are posting no data. 在您的Ajax请求中,您没有发布任何数据。

Assuming you are using this plugin http://docs.jquery.com/Plugins/autocomplete 假设您正在使用此插件http://docs.jquery.com/Plugins/autocomplete

try using 尝试使用

$("#tbSearch").autocomplete("AgentList.aspx/LoadData");

and returning an array in your web service 并在您的Web服务中返回一个数组

I'm making a few assumptions here, but it looks like you're using the autocomplete plugin incorrectly. 我在这里做一些假设,但看来您使用的是自动填充插件错误。 You are attaching to the change event on a textbox, and based on the name of that textbox I'm assuming you're looking to use autocomplete for search suggestions based on what the user inputs. 您将附加一个文本框上的change事件,并基于该文本框的名称,假设您要根据用户输入的内容使用自动填充功能进行搜索。

If that is the case, then you simply need to attach the autocomplete functionality to the textbox in this way: 如果是这种情况,那么您只需要以这种方式将自动完成功能附加到文本框:

$(document).ready(function() {
    $("#tbSearch").autocomplete("AgentList.aspx/LoadData");
});

The plugin will take care of the AJAX request — you don't have to. 该插件将处理AJAX请求-您不必这样做。

Remember that in ASP.NET the ID of the control is used when referring to it in the code file or code behind, and isn't the actual ID of the input field when rendered to HTML. 请记住,在ASP.NET中,在代码文件或后面的代码中引用控件时,将使用该控件的ID,并且在呈现为HTML时,它不是输入字段的实际ID。 View the source of your page to see the actual ID. 查看页面的源以查看实际的ID。 You probably want something like this: 您可能想要这样的东西:

$("#<%= tbSearch.ClientID %>").autocomplete({
...

or 要么

$("input.autocomplete").autocomplete({
...

<asp:TextBox ID="tbSearch" runat="server" CssClass="autocomplete"/>

Hope that helps. 希望能有所帮助。

Your asp:TextBox , while having an ID set to "tbSearch", will not be exactly that when it goes to your browser. 您的asp:TextBox的ID设置为“ tbSearch”时,与您的浏览器时不完全相同。 ASP.NET adds a bit to your ID that helps it identify your control server-side. ASP.NET会在您的ID中添加一些内容,以帮助其识别控制服务器端。

Try this instead: 尝试以下方法:

$('[id$="tbSearch"]').autocomplete({
    source : availableTags
});

Fully functioning example 功能齐全的例子

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <link rel="stylesheet" href="http://dev.jquery.com/view/trunk/plugins/autocomplete/jquery.autocomplete.css" type="text/css" />
    <script type="text/javascript" charset="utf-8" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js">
    </script>
    <script type="text/javascript" charset="utf-8" src="http://dev.jquery.com/view/trunk/plugins/autocomplete/jquery.autocomplete.js"></script>
    <script type="text/javascript" charset="utf-8">
      $(document).ready(function() {        
         var availableTags = [
            "ActionScript",
            "AppleScript",
            "Asp",
            "BASIC",
            "C",
            "C++",
            "Clojure",
            "COBOL",
            "ColdFusion",
            "Erlang",
            "Fortran",
            "Groovy",
            "Haskell",
            "Java",
            "JavaScript",
            "Lisp",
            "Perl",
            "PHP",
            "Python",
            "Ruby",
            "Scala",
            "Scheme"
        ];
        $('#tbSearch').autocomplete(availableTags);
      });
    </script>
  </head>
  <body>
    <input type="text" id = "tbSearch" name="code">
  </body>
</html>

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

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