不确定是否可以更新此jQuery以针对ASP填充数组进行验证。
下面,我使用jQuery中的MIDlist进行验证,但现在我想使用ASP填充的数组进行验证。
我的原始验证列表如下;
var MIDlist = ['12345','90210','12346','12347'];
使用从下面的asp连接创建的填充数组,我想用我的MSSQL数据库中的数据列表替换['12345','90210','12346','12347']。
var MIDlist = ['12345','90210','12346','12347'];
function validateMID() {
return $.inArray($('#MID').val(), MIDlist) > -1;
//true means the MID is in the list, false it is not
}
$(function() { // Shorthand for $(document).ready(function() {
$('#MID').keyup(function() {
if( $('#MID').val().length == 5 ) {
if (!validateMID()) {
// Good, MID is allowed
$('#MID').removeClass('red');
$('p').text('MID does not exist.');
} else {
// MID already exist
$('#MID').addClass('red');
$('p').text('MID was found in the list! Do not use.');
}
}
});
});
这是我现在要针对其进行验证的ASP选择语句。
<%
Dim MID_LIST
Dim MID_LIST_cmd
Dim MID_LIST_numRows
Set MID_LIST_cmd = Server.CreateObject ("ADODB.Command")
MID_LIST_cmd.ActiveConnection = MM_ServerConnection_STRING
MID_LIST_cmd.CommandText = "SELECT MID FROM dbo.tblCustomer WHERE intActiveStatus = 1 and intCancelled = 0 and intMarkAsRemovedFlag = 0"
MID_LIST_cmd.Prepared = true
Set MID_LIST = MID_LIST_cmd.Execute
MID_LIST_numRows = 0
%>
那么我如何修改上面的ASP代码以创建一个填充数组并让jQuery读取该数组?
谢谢,雷。
答案 0 :(得分:1)
首先-我假设您了解您的jQuery代码是客户端,而ASP VBScript代码是服务器端。这意味着jQuery代码看不到VBScript,只能看到输出。
我认为您要替换行
var MIDlist = ['12345','90210','12346','12347'];
具有由经典ASP代码填充的数组
<%
'First of all you open a connection to the database
dim conn
set conn = Server.Createobject("ADODB.Connection")
conn.Open MM_ServerConnection_STRING
'Then create a recordset object
dim rs
set rs = Server.CreateObject("ADODB.Recordset")
'And a database query to populate the recordset
dim query
query = "SELECT MID FROM dbo.tblCustomer WHERE intActiveStatus = 1 and intCancelled = 0 and intMarkAsRemovedFlag = 0"
'open your recordset
rs.open query,conn
'and loop through it to build the array you want to use in your jQuery code
dim myJqueryArray
myJqueryArray = ""
Do until rs.eof
myJqueryArray = myJqueryArray & "'" & rs("MID") & "'"
rs.movenext
myJqueryArray = myJqueryArray & ","
loop
'tidy up
set rs = nothing
set conn = nothing
%>
请注意,逗号在movenext指令之后插入,这样可以防止在数组的最终值之后添加逗号。
完成此操作后,您可以将
替换为jQuery代码的第一行var MIDlist = [<%= myJqueryArray %>];
创建记录集是Classic ASP中的常见任务,您应该在Internet上找到很多有关如何进行记录的教程。我建议您这样做,而不是使用Dreamweaver生成代码,因为它非常肿且难以理解。