Classic-ASP形式的可变数量的输入字段 [英] Variable Number of Input Fields in Classic-ASP form
问题描述
我有一个结帐表格,其中产品数量可以为"n".那么我如何知道表单中有多少个输入字段并从中获取输入呢?
I have a check out form where number of products can be "n". So how i can know how many input fields are in the form and take input from it?
谢谢
推荐答案
如果它是一组单个控件(例如,表示项目的复选框数量可变),则解决方案非常简单.对于您的复选框:
If it's a group of single controls - say a variable number of checkboxes representing items - the solution is pretty straightforward. For your checkboxes:
<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
然后在您的ASP中,您可以执行以下操作:
Then in your ASP, you could do this:
<%
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
%>
实际上,这种方法适用于任何数量的具有相同名称的控件.如果名称为"FavoriteColor"的文本框为1-n个,则可以用相同的方式通过For Each
遍历每个值.没有用户输入的文本框将不会被传递.
In fact, this approach will work for any number of controls with the same name. If it were 1 - n number of textboxes with the name "FavoriteColor", you could For Each
through each value in the same manner. Textboxes with no user input would not be passed.
现在,如果您的结帐表单中每个项目都包含一组输入控件,则可以通过仔细命名其他控件来建立该方法:
Now, if your checkout form contains a group of input controls per item, you can build on that approach by carefully naming your other controls:
<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>
再次,现在,您可以在服务器上以这种方式解析数据:
Now, again, on the server you could parse the data in this way:
<%
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
%>
您将能够以这种方式对每组所选项目执行验证和其他逻辑.
You'd be able to perform validation and other logic on each group of selected items in this manner.
这篇关于Classic-ASP形式的可变数量的输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!