引用未选中复选框使用VBScript导致错误 [英] Referencing unchecked checkboxes with vbscript causes an error

查看:216
本文介绍了引用未选中复选框使用VBScript导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了我的VBScript code的一个问题。我的HTML code看起来像这样

I am running into a problem with my vbscript code. My HTML code looks like this

<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的复选框。

This is done because above there is another checkbox that calls a javascript function that will check or uncheck all of the "DisplayRow" checkboxes. The javascript function uses getElementsByName to return all of the checkboxes named "DisplayRow".

在单击窗体的提交按钮的动作将其发送到ASP页(传统的ASP),通过使用该命令的Request.Form抓住所有的调用窗体上的对象。该命令的Request.Form着眼于名属性,而不是对象的id属性。

When the form's submit button is clicked the action sends it to an ASP page (classic ASP) that grabs all of the objects on the calling form by using the Request.Form command. The Request.Form command looks at the "name" attribute, not the "id" attribute of the object.

这似乎是工作的罚款所有其他形式的对象。当它到达的复选框,因为名称属性使用相同的名称为所有的复选框,则返回一个数组。单个复选框可以这样访问:

This seems to be working fine for all of the other form objects. When it gets to the checkboxes because the "name" attribute uses the same name for all of the check boxes it returns an array. The individual checkboxes can be accessed like this:

Request.Form("DisplayRow")(x)

其中x引用单个复选框阵列中

Where x references the individual checkbox in the array.

如果该复选框被选中,我可以通过没有任何问题数组循环。如果1个或多个或所有的复选框都未选中那么当code引用了第一个复选框即未被选中的页面崩溃的数组中为止。在的Request.Form命令失败后执行任何操作。

If the checkboxes are checked I can loop through the array without any problems. If 1 or more or all of the checkboxes are unchecked then when the code references the first checkbox in the array that is unchecked the page crashes. Nothing is executed after the Request.Form command fails.

我试图封闭的Request.Form(DisplayRow),(十)在if语句中的ISNULL功能,但它仍然需要程序了。

I have tried enclosing the Request.Form("DisplayRow")(x) in an IsNull function in an If statement but it still takes the program down.

有没有其他人遇到了这一点,并发现周围的工作?

Has anyone else ran into this and found a work around?

修改

由于某些原因计算器是不是让我添加多个注释。

For some reason stackoverflow is not letting me add more than one comment.

@Cory。感谢您的信息。

@Cory. Thanks for the information

@ jwatts1980。伯爵的作品,但它不会让我知道其中的复选框被选中。如果计数大于0,我可以通过他们,但循环,如果第一个被选中,我马上回来,我开始崩溃的页面。

@jwatts1980. Count works but it does not let me know which of the checkboxes are checked. If the count is greater than 0 I can loop through them but if the first one is unchecked I am right back where I started with a crashed page.

推荐答案

您不能做这种方式,因为选中复选框不会在后提交,只有那些检查。

You cannot do it this way because unchecked checkboxes will not be submitted in the post, only the checked ones.

我将以不同的方式接近这一点:

I would approach this differently:

首先我会属性添加一个价值的复选框,像这样:

First I would add a value attribute to the checkboxes, as so:

<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" />

注意该值是一样的指数,这将需要在code

Notice the value is the same as the index, this will be needed in the code.

接下来我会使用 .Split()聚集在阵列中的复选框的选中,因为值将作为过来用逗号,即分隔字符串: 1,2,3 你的 .FORM()值。

Next I would use .Split() to gather the checkboxes selected in an array, since the values will come in as a string separated by comma, ie: 1,2,3 to your .Form() value.

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

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

This way you only process the results for the one's selected, and ignore the others.

这篇关于引用未选中复选框使用VBScript导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆