gpt4 book ai didi

asp-classic - Classic-ASP 表单中可变数量的输入字段

转载 作者:行者123 更新时间:2023-12-01 08:41:11 26 4
gpt4 key购买 nike

我有一个结帐表格,其中产品的数量可以是“n”。那么我如何知道表单中有多少输入字段并从中获取输入呢?

谢谢

最佳答案

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

<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 中,您可以这样做:

<%
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
%>

事实上,这种方法适用于任意数量的同名控件。如果它是 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>

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

<%
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
%>

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

关于asp-classic - Classic-ASP 表单中可变数量的输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2839227/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com