自动填充文本框未进入下一条记录

时间:2016-03-24 02:50:28

标签: javascript jquery asp-classic

我使用下面的代码来获取文本框中的信息,但是当它运行时它只显示数据库的第一条记录。我怎样才能获得下一条记录?

...的VBScript

Dim Leads
Dim Leads_cmd
Dim Leads_numRows
Set Leads_cmd = Server.CreateObject ("ADODB.Command")
Leads_cmd.ActiveConnection = MM_PSCRM_STRING
Leads_cmd.CommandText = "SELECT lead_desc from dba.lead" 
Leads_cmd.Prepared = true
Set Leads = Leads_cmd.Execute
Leads_numRows = 0
...
...
'At end of file...
Leads.Close()
Set Leads = Not

JavaScript部分......

$(function() {
var availableTags = ["<%=(Leads.Fields.Item("lead_desc").Value)%>"];
$( "#tags" ).autocomplete({
source: availableTags
});
  });

HTML部分......

<div class="ui-widget">
    <label for="tags">Tags: </label>
    <input id="tags" name="NameCus">
</div>

3 个答案:

答案 0 :(得分:1)

您可能最好使用GetRows()将数据读入数组。然后,您可以使用此二维数组在线填充JavaScript代码...

Dim rs, sql, res
sql = "SELECT lead_desc from dba.lead"
Set rs = Server.CreateObject("ADODB.RecordSet")
rs.Open sql, MM_PSCRM_STRING, 0, 1, 1
res = rs.GetRows()
rs.Close()
Set rs = Nothing
Dim c, arr
For c = 0 To UBound(res, 2)
    arr = arr & res(0, c) & ""","""
Next
arr = """" & Left(arr, Len(arr)-1) & """" 'Chop off the last comma

(由Martha提醒):还有RecordSet对象的GetString method

Dim rs, sql, res
sql = "SELECT lead_desc from dba.lead"
Set rs = Server.CreateObject("ADODB.RecordSet")
rs.Open sql, MM_PSCRM_STRING, 0, 1, 1
arr = rs.GetString(,,,",")
rs.Close()
Set rs = Nothing

(我假设您正在使用SQL Server):如果您在SQL中执行此操作,则可以使用STUFF方法将字符串编译到列表中。不过,我建议您使用存储过程。

Dim rs, sql, res
sql = _
    "SELECT STUFF((" & _
        "SELECT " & _
            "',' + lead_desc " & _
        "FROM " & _
            "dba.lead " & _
        "ORDER BY " & _
            "dba.lead " & _
        "FOR XML PATH('')), 1, 1, '') " 
Set rs = Server.CreateObject("ADODB.RecordSet")
rs.Open sql, MM_PSCRM_STRING, 0, 1, 1
res = rs.GetRows()
rs.Close()
Set rs = Nothing

你的JavaScript ......

var availableTags = [<%= arr %>];

答案 1 :(得分:0)

将您的javascript代码更改为以下内容:

<script>
$(function() {
var availableTags = ["<%
Do Until Leads.EOF
 Response.Write(Leads.Fields.Item("lead_desc").Value & ",")
 Leads.Movenext 
Loop
%>"];
$( "#tags" ).autocomplete({
  source: availableTags
  });
});
</script>

这是因为你忘了在循环中执行语句。因此,只有第一个元素被加载到自动完成中。希望这会有所帮助:)

答案 2 :(得分:0)

感谢大家的支持。 我是这样做的

<script type="text/javascript" language="javascript">
<!--
    $(function() {
$( "#productname" ).autocomplete({
source: "list.asp",
        minLength: 2
    });
   });
// -->
</script>