0

在两种情况下,我从 Jquery 的简单自动完成开始:本地数组和来自其他页面的远程源数据。我确信两者的数据相同,但自动完成的工作方式不同。请参阅下面的代码:

1.带本地数组

Javascript代码

$(document).ready(function() {
        $("#txtArr").autocomplete({
        source: programmingLang,
        change:function(event, ui){
            if (ui.item==null)
                $('#txtArr').val(-1);
        }
        })
})

编程语言数组:

var programmingLang = [{ "value": "ActionScript", "id": 31 }, { "value": "AppleScript", "id": 2 }, { "value": "JavaScript", "id": 3 }, { "value": "Haskell", "id": 33 }, { "value": "Architects", "id": 27 }, { "value": "Scheme", "id": 1 }, { "value": "PHP", "id": 29 }, { "value": "Marketing", "id": 25 }, { "value": "Perl", "id": 15 }, { "value": "Training", "id": 32}];

从浏览器中,我输入了 p,...,列表中显示的包含“p”的项目 - 完美:)

2.通过源数据远程

Javascript代码

<script type="text/javascript">
    $(document).ready(function() {
        $("#txtArr").autocomplete({
        source: "script.asp",
        change:function(event, ui){
            if (ui.item==null)
                $('#txtArr').val(-1);
        }
        })
})

脚本.asp

<%    
Response.ContentType = "application/json; charset=utf-8"    
Response.Write("[{ ""value"": ""ActionScript"", ""id"": 31 }, { ""value"": ""AppleScript"", ""id"": 2 }, { ""value"": ""JavaScript"", ""id"": 3 }, { ""value"": ""Haskell"", ""id"": 33 }, { ""value"": ""Architects"", ""id"": 27 }, { ""value"": ""Scheme"", ""id"": 1 }, { ""value"": ""PHP"", ""id"": 29 }, { ""value"": ""Marketing"", ""id"": 25 }, { ""value"": ""Perl"", ""id"": 15 }, { ""value"": ""Training"", ""id"": 32}]")
%>

从浏览器:键入 p ... 列出数组中的所有项目 :(

如何修复它们与本地数组的工作方式相同?

非常感谢。


现在可以了。我修改script.asp类似于elliottjmills的建议

 <%    
Response.ContentType = "application/json; charset=utf-8"
strSql="SELECT * FROM HR_Employee WHERE **Fullname like '%" & request.QueryString("term")** & "%'"   

strconn="PROVIDER=SQLOLEDB;DATA SOURCE=...;DATABASE=...;USER ID=...;PASSWORD=...;"  

set conTem=Server.CreateObject("ADODB.Connection")
conTem.Open(strconn)

Set rsElementTem = Server.CreateObject("ADODB.Recordset")
rsElementTem.Open strSql,conTem,3,3
strArr=""
if not rsElementTem.EOF then
    rsElementTem.MoveFirst
    'rsElementTem.Filter="Fullname like '*" & **request.QueryString("term")** & "*'"

    do while not rsElementTem.EOF
        if strArr<>"" then strArr=strArr & ","
        strArr=strArr & "{"&_
                        """value"":""" & rsElementTem("Fullname") & """, ""id"":" & rsElementTem("PersonID") & "}" 

        rsElementTem.MoveNext
    loop
end if


Response.Write "[" & strArr & "]"

%>

4

2 回答 2

1

这应该做一些类似于你所追求的事情:

脚本.asp:

<%
Response.ContentType = "application/json; charset=utf-8"

Dim value(6)
Dim id(6)

value(0) = "dog"
id(0) = 1
value(1) = "cat"
id(1) = 20
value(2) = "pig"
id(2) = 31
value(3) = "monkey"
id(3) = 25
value(4) = "camel"
id(4) = 16
value(5) = "carrot"
id(5) = 28
value(6) = "donkey"
id(6) = 9

Dim jsonString
jsonString = "["

For i = 0 to Ubound(value)
    If(left(value(i), Len(Request.Querystring("term"))) = Request.Querystring("term")) Then
    jsonString = jsonString & " { ""value"": """ & value(i) & """, ""id"": " & id(i) & " },"
    End If
Next

jsonString = Left(jsonString, Len(jsonString)-1) & " ]"
Response.Write jsonString
%>

这会将term查询字符串中的参数与数组中每个元素的开始字符进行比较。

对于每个匹配,它会将值添加到序列化格式的字符串中。

最后它会用函数去掉结尾的逗号Len,关闭方括号并输出字符串的内容!

于 2012-07-20T11:14:52.303 回答
0

您应该从服务器端带来排序列表

于 2012-07-20T04:43:36.390 回答