关于 html:使用 vbscript 引用未选中的复选框会导致错误

Referencing unchecked checkboxes with vbscript causes an error

我的 vbscript 代码遇到了问题。我的 HTML 代码如下所示

1
2
3
<input type='checkbox' name='DisplayRow' id='DisplayRow1' />
<input type='checkbox' name='DisplayRow' id='DisplayRow2' />
<input type='checkbox' name='DisplayRow' id='DisplayRow3' />

这样做是因为上面还有另一个复选框调用了一个 javascript 函数,该函数将选中或取消选中所有"DisplayRow"复选框。 javascript 函数使用 getElementsByName 返回所有名为 "DisplayRow" 的复选框。

当单击表单的提交按钮时,操作会将其发送到一个 ASP 页面(经典 ASP),该页面使用 Request.Form 命令获取调用表单上的所有对象。 Request.Form 命令查看对象的"name"属性,而不是"id"属性。

这似乎适用于所有其他表单对象。当它到达复选框时,因为 "name" 属性对所有复选框使用相同的名称,它返回一个数组。可以像这样访问各个复选框:

1
Request.Form("DisplayRow")(x)

其中 x 引用数组中的单个复选框。

如果选中复选框,我可以毫无问题地遍历数组。如果未选中一个或多个或所有复选框,则当代码引用数组中未选中的第一个复选框时,页面崩溃。 Request.Form 命令失败后不执行任何操作。

我已尝试将 Request.Form("DisplayRow")(x) 包含在 If 语句中的 IsNull 函数中,但它仍会使程序停止运行。

有没有其他人遇到过这个问题并找到了解决方法?

编辑

由于某种原因,stackoverflow 不允许我添加多个评论。

@科里。感谢您的信息

@jwatts1980。计数有效,但它不会让我知道选中了哪些复选框。如果计数大于 0,我可以遍历它们,但如果未选中第一个,我就回到了我从崩溃页面开始的地方。


您不能这样做,因为未选中的复选框不会在帖子中提交,只会提交选中的复选框。

我会以不同的方式处理这个问题:

首先我会在复选框中添加一个 value 属性,如下所示:

1
2
3
<input type='checkbox' name='DisplayRow' id='DisplayRow1' value="1" />
<input type='checkbox' name='DisplayRow' id='DisplayRow2' value="2" />
<input type='checkbox' name='DisplayRow' id='DisplayRow3' value="3" />

注意该值与索引相同,这将在代码中使用。

接下来我将使用 .Split() 来收集数组中选中的复选框,因为这些值将以逗号分隔的字符串形式出现,即:1,2,3 到您的 .Form() 值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Rows = Request.Form("DisplayRow")

'check if any are selected first
If Rows <>"" Then

  'make an array with each value selected
  aRows = Split(Rows,",")

  'loop through your array and do what you want
  For i = lBound(aRows) to uBound(aRows)
    Response.Write"DisplayRow" & aRows(i) &" was Selected."
  Next

End If

这样你只处理选择的结果,而忽略其他的。


我一直觉得这对我的图书馆很重要...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'GetPostData
'   Obtains the specified data item from the previous form get or post.
'Usage:
'   thisData = GetPostData("itemName","Alternative Value")
'Parameters:
'   dataItem (string) - The data item name that is required.
'   nullValue (variant) - What should be returned if there is no value.
'Description:
'   This function will obtain the form data irrespective of type (i.e. whether it's a post or a get).
'Revision info:
'   v0.1.2 -    Inherent bug caused empty values not to be recognised.
'   v0.1.1 -    Converted the dataItem to a string just in case.
function GetPostData(dataItem, nullVal)
    dim rV
    'Check the form object to see if it contains any data...
    if request.Form ="" then
        if request.QueryString ="" then
            rV = nullVal
        else
            if request.QueryString(CStr(dataItem))="" then
                rV = CStr(nullVal)
            else
                rV = request.QueryString(CStr(dataItem))
            end if
        end if
    else
        if request.Form(CStr(dataItem)) ="" then
            rV = CStr(nullVal)
        else
            rV = request.Form(CStr(dataItem))
        end if
    end if
    'Return the value...
    GetPostData = rV
end function

要使用该函数,您只需传入您要查找的表单或查询字符串项以及用什么替换空值...

1
2
Dim x
x = GetPostData("chkFirstOne", false)


您可以通过添加单个条件来继续使用现有代码:

1
2
3
4
5
If Request.Form("DisplayRow").Count > 0 Then
    'your code here
Else  
    'consider adding message saying nothing was selected
End If