在两种情况下,我从 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 & "]"
%>