如何连接自动完成到一个文本框? [英] How do I connect an autocomplete to a textbox?

查看:263
本文介绍了如何连接自动完成到一个文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与此相关的: jQuery的犯规去错误或成功

我已经得到了我需要自动完成添加一个文本框的老1.1 asp.net/vb.net项目。
我写了一个的.asmx(Web服务的文件)这样:

I've got an old 1.1 asp.net/vb.net project that I need to add autocomplete to a textbox. I wrote a .asmx (the web service file) as such:

 <WebMethod()> _
    Public Function GetTags() As String()
        Dim arr() As String = BindTags()
        Return arr
    End Function
    Private Function BindTags() As String()
        Dim cmdSelect As SqlCommand
        Dim conMyData As SqlConnection
        Dim reader As SqlDataReader
        Dim myList As New ArrayList

        'try and make a connection   
        Try
            conMyData = New SqlConnection(ConfigurationSettings.AppSettings("strConn"))
            cmdSelect = New SqlCommand("select_tags_grid", conMyData)

            With cmdSelect
                .CommandType = CommandType.StoredProcedure
                'add parameters
                .Parameters.Add("@SortOrder", SqlDbType.TinyInt).Value = 1
                'check the clientid
                conMyData.Open()
                reader = cmdSelect.ExecuteReader(CommandBehavior.CloseConnection)
            End With

            While (reader.Read())
                myList.Add(CType(reader("Tag"), String))
            End While

            Dim arr() As String = CType(myList.ToArray(Type.GetType("System.String")), String())
            Return arr
        Catch e As Exception
            'clean up and close resources
            Throw e
        Finally
            cmdSelect = Nothing
            conMyData.Close()
            conMyData = Nothing
        End Try
    End Function

这工作得很好,我可以看到的数据,当我运行这个.asmx文件。然后,我在这说.NET 1.1不支持JSON / JSONP格式,并使用XML文章阅读的文章。于是我又踏上了jQuery方这个自动完成用户界面连接到我的文本框。这里是我的尝试:

This works fine as I can see the data when I run this .asmx file. Then I've read articles upon articles that say .net 1.1 did not support json / jsonp format and to use xml. So then I went on to embark on the jquery side to attach this autocomplete ui to my textbox. Here is what I tried:

$("#txtTags").autocomplete({
    minLength: 0,
    source: function(request, response) {   
        $.ajax({
            type: "POST",
            url: "GetTags.asmx/GetTags",
            dataType: "xml",
            contentType: "text/xml; charset=utf-8",
      success: function(xml) {
           alert("hi");
           // Completion logic goes here
      },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(textStatus);
            }
        });
    },    
});

现在,当我运行我的应用程序,并使用例如谷歌浏览器我没有看到开发人员工具控制台的任何错误弹出,当我在文本框中输入。所以我不知道这是工作或没有。我试图按照这个计算器的答案: http://stackoverflow.com/a/7729147/168703
看这家伙是怎么做到的,我pretty肯定我也跟着正确的?谁能告诉我在做什么错吧。

Now when I run my app and use for instance google chrome I do not see any errors in the developer tools console pop up when I type in the textbox. So I am not sure if this is working or not. I tried to follow this stackoverflow answer: http://stackoverflow.com/a/7729147/168703 to see how this guy did it and i'm pretty sure I followed correctly? Can anyone tell what I am doing wrong please.

推荐答案

在Chrome浏览器的工具去网络选项卡。使用清除按钮来删除所有条目,然后开始在文本框中输入。如果自动完成工作,一个条目应该会出现在你的面前(网络选项卡上),然后单击它应该给你上正在发生的事情的细节。

On chrome's tools go to the Network tab. Use the "clear" button to delete all entries and then start typing in your textbox. If autocomplete is working, an entry should appear in front of you (on the network tab) and clicking on it should give you details on what is going on.

在上面的例子中,我发现了调用通用处理我使用的自动完成功能。既然你是在1.1和您使用的Web服务(如果我unserstood正确的),你应该看到Web服务调用或类似的东西。

In the above example I'm getting the call to the generic handler I use for the autocomplete. Since you are on 1.1 and you use a web service (if I unserstood correctly) you should see the web service call or something similar.

这篇关于如何连接自动完成到一个文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆