Classic-ASP形式的可变输入字段数

its*_*ode 3 asp-classic

我有一个结帐表格,产品数量可以是"n".那么我怎么知道表单中有多少输入字段并从中获取输入?

谢谢

Bra*_*ing 10

如果它是一组单个控件 - 比如表示项目的可变数量的复选框 - 解决方案非常简单.对于您的复选框:

<input type="checkbox" name="ProductID" value="1" />Product #1<br />
<input type="checkbox" name="ProductID" value="2" />Product #2<br />
<input type="checkbox" name="ProductID" value="3" />Product #3
Run Code Online (Sandbox Code Playgroud)

然后在你的ASP中,你可以这样做:

<%
  Dim intID

  For Each intID In Request.Form("ProductID")
    ' intID now represents a selected product ID.  Insert into DB
    ' or whatever your process is.  Note that only the "checked" values
    ' will be passed to the server.
  Next
%>
Run Code Online (Sandbox Code Playgroud)

实际上,这种方法适用于任意数量的同名控件.如果它是1-n个名为"FavoriteColor"的文本框,则可以For Each以相同的方式浏览每个值.不传递没有用户输入的文本框.

现在,如果您的结帐表单包含每个项目的一组输入控件,您可以通过仔细命名其他控件来构建该方法:

<div>
<input type="checkbox" name="ProductID" value="1" />Product #1<br />
<input type="textbox" name="Product1_Quantity">
<input type="textbox" name="Product1_Color">
</div>

<div>
<input type="checkbox" name="ProductID" value="2" />Product #2<br />
<input type="textbox" name="Product2_Quantity">
<input type="textbox" name="Product2_Color">
</div>

<div>
<input type="checkbox" name="ProductID" value="3" />Product #3
<input type="textbox" name="Product3_Quantity">
<input type="textbox" name="Product3_Color">
</div>
Run Code Online (Sandbox Code Playgroud)

现在,再一次,在服务器上,您可以用这种方式解析数据:

<%
  Dim intID
  Dim intQuantity
  Dim strColor

  For Each intID In Request.Form("ProductID")
    ' this is a selected item
    intQuantity = Request.Form("Product" & intID & "_Quantity")
    strColor = Request.Form("Product" & intID & "_Color")
  Next
%>
Run Code Online (Sandbox Code Playgroud)

您将能够以这种方式对每组选定项目执行验证和其他逻辑.