根据jquery ui API,自动完成字段的来源可能是数组变量。如何使用vbscript记录集结果填充数组变量?
在我的页面正文中,我的代码是这样的:
Dim rs, data_source
Set rs = Server.CreateObject("ADODB.Recordset")
rs.ActiveConnection = MM_rtasql_STRING
rs.CursorType = 2
rs.CursorLocation = 2
rs.LockType = 3
rs.source = "select field1 from mydatabase where whatever > soandso"
WHILE NOT rs.EOF
//add each result to string array JavaScript/Jquery can access.
我想在我的文档就绪函数中为我的jquery auto complete字段提取变量。如何实施?
谢谢。
答案 0 :(得分:0)
http://jqueryui.com/autocomplete/#remote远程数据源。使用带有firebug的firefox时,按F12打开控制台,看到输入时输入的xhr请求。
检查响应我可以看到使用了JSON:
[{"id":"Turdus philomelos","label":"Song Thrush","value":"Song Thrush"},
{"id":"Melospiza melodia","label":"Song Sparrow","value":"Song Sparrow"}
]
我认为你可以省略id,因为那是可选的。您可以尝试返回一个字符串数组:
["Song Thrush","Song Sparrow"]
您显示的vb代码看起来像服务器端代码不能在浏览器中运行,自动完成将在用户输入内容时调用该asp页面,然后用于过滤数据:
自动完成发送的GET参数称为term。在jquery ui页面上查找js代码:http://jqueryui.com/autocomplete/#remote
答案 1 :(得分:0)
你可以这样做,假设你很高兴在页面加载时填充列表,并且不想实时查找服务器:
<%
' server-side ASP
Dim rs, data_source, ado
Set ado = CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")
ado.ConnectionString = MM_rtasql_STRING
ado.Open
rs.ActiveConnection = ado
rs.CursorType = 2
rs.CursorLocation = 2
rs.LockType = 3
rs.Open "select field1 from mydatabase where whatever > soandso"
' build up a string which looks like a Javascipt array (eg "['val1','val2']")
' instead of a loop, you could use rs.GetString(), which is quicker
dim s: s = "["
do until rs.EOF
s = s & "'" & CStr(rs("field1")) & "',"
loop
rs.Close
' remove trailing comma and complete array string
s = left(s, len(s) - 1) & "]"
%>
<!-- client-side -->
<script type="text/javascript">
$(document).ready(function() {
$("textbox-selector").autocomplete({
source: <%=s %> // ASP insert of javascript array string
});
});
</script>
我没有测试过这个和我从他们的例子中得到的jQuery:http://jqueryui.com/autocomplete/
答案 2 :(得分:-1)
使用push()方法怎么样?