我有一堆复选框,用户可以从他们想要加载的系统列表中进行选择。复选框的创建方式如下:无法访问复选框属性(客户端VBScript)
'***************************************************************
' Print systems from DB in a table for the load page.
'***************************************************************
Function PrintSystemTable()
Set con = CreateObject("ADODB.Connection")
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & systemsDBPath
Set systemRs = CreateObject("ADODB.Recordset")
systemRs.CursorLocation = adUseClient
Set sensorRs = CreateObject("ADODB.Recordset")
sensorRs.CursorLocation = adUseClient
document.write("<table class=satelliteList>")
document.write("<tr class=""tableHeader"">")
document.write("<td>Select</td>")
document.write("<td width=""100"">System Name</td>")
document.write("<td width=""100"">Description</td>")
document.write("<td width=""75"">Files</td>")
document.write("</tr>")
systemRs.Open "SELECT * FROM System ORDER BY SystemName", _
con, adOpenStatic, adLockOptimistic
do until systemRs.eof
document.write("<tr valign=""top"">")
document.write("<td>")
document.write("<INPUT TYPE=CHECKBOX NAME=""system"" VALUE=""" & systemRs("SystemName") & """>")
document.write("</td>")
document.write("<td>")
document.write(systemRs("SystemName"))
document.write("</td>")
document.write("<td>")
document.write(systemRs("Description"))
document.write("</td>")
document.write("<td>")
document.write(guiPath & systemRs("Satellite"))
sensorRs.Open "SELECT * FROM SystemSensors WHERE System='" & systemRs("SystemName") & "'", _
con, adOpenStatic, adLockOptimistic
do until sensorRs.eof
document.write("<br>" & guiPath & sensorRs("Sensor"))
sensorRs.movenext
loop
sensorRs.Close
document.write("</td>")
'adoDBRecordset("FieldName") & "<br>")
document.write("</tr>")
systemRs.movenext
loop
document.write("</table>")
con.Close
End Function
它们不在表单中。我试图通过他们的名字(系统)访问我的复选框并遍历它们以查看哪些被检查。
'find the systems that are selected
'access an array of all the checkboxes?
Dim checkboxes
Set checkboxes = document.getElementsByName("system")
'For i=0 to UBound(checkboxes)
For Each chk in checkboxes
' If (checkboxes(i).checked = true) Then
If (chk.checked = true) Then
document.write("A checkbox is checked <br>")
Else
document.write("Unchecked <br>")
End If
'document.write(chk.value & "<br />")
Next
我已经尝试迭代使用for和for each循环。无论哪种方式我都会得到同样的错误这里是发生了什么:
现在我在我的数据库中有两个测试样本,所以我得到两件事我可以选择。如果我选择第一个项目并按下运行我的检查脚本的按钮,则会打印“检查复选框”,并在我的页面上显示错误(权限被拒绝),表示我无法访问检查的属性。它不打印“未选中”。如果我选择我的第二个项目,不选中第一个项目,它会打印“未选中”,而不打印“已选中复选框”。
如果我选择这两个项目,它会打印“检查复选框”一次,并给出相同的错误。
如果我不选择任何一项打印“未检查”ONCE(而不是每个未检查项目一次),并且我得到相同的权限拒绝错误告诉我我无法访问检查的属性。
在我开始测试选中的值之前,我尝试打印每个复选框的值,但试图访问value属性也给了我权限被拒绝。
奇怪的是,它似乎是“工作”的第一个元素(因为它只会打印一个元素适当的消息),即使它仍然抛出权限被拒绝的错误。这导致我相信我要么不正确地使用document.getElementsByName()(它是否返回一个数组?),要么我没有正确地迭代......或者其他一些超越我的神秘事物。
这是CLIENT-SIDE vbscript,不是ASP。 Javascript也不是一个选项;出于特定原因,我使用HTML的客户端vbscript。不要因为我使用它而告诉我这是愚蠢的,因为我同意;我没有选择。
如何访问和遍历我的同名复选框(系统),并查看哪些被选中并获取它们的值?
太棒了!谢谢!我甚至没有想到document.write会中断其余的代码... – Lauren 2010-07-23 13:30:49