我有一个文本框。我想循环遍历列表元素并将其值发布到数据库。我怎么能在asp中做到这一点?
Set dataCon = Server.CreateObject("ADODB.Connection")
Set rsAwards = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT * FROM Awards"
rsAwards.Open strSQL, dataCon
<form name="sort_award" action="action_awards.asp?Action=sort&Award_ID=<%=Award_ID%>" method="post">
response.write "<ul id='sortable'>"
dim i
i=0
While not rsAwards.EOF
response.write "<li onclick='sort()'><input type='text' id='AwardNum' size='1' value="&i&" ></li>"
rsAwards.MoveNext
i=i+1
Wend
response.write "</ul></form>"
答案 0 :(得分:1)
可能出现的问题(不确定这些是否真的存在问题,或者仅仅是修剪代码的工件):
<form action="action_awards.asp?Action=sort&Award_ID=<%=Award_ID%>" method="post">
<%
dim i
i=0
'... display stuff omitted
While not rsAwards.EOF
'- only fields with names can return any data
response.write "<input type='text' name='A_" & i & "'"
'- id attributes must be unique
response.write " id='AwardNum_" & i & "' size='1' value='" & i & "'>"
rsAwards.MoveNext
i=i+1
Wend
'- tell the form how many fields there are
response.write "<input type='hidden' name='N' value='" & i & "'>"
'- (I assume you have a submit button or some other method to submit the data?)
%>
</form>
要阅读此表单,您需要执行以下操作:
dim N, V(), i
N = request.form("N")
redim V(N)
for i = 0 to N
V(i) = request.form("A_" & i)
next
另一种选择是给所有字段命名:
response.write "<input type='text' name='Award' value='" & i & "'>"
这将为您提供以逗号分隔的值列表,您可以使用Split()
来对其进行分割。
V = Request.Form("Award")
V = Split(V,", ")
for i = 0 to UBound(V)
if V(i) <> "" then '... do stuff ...
next
请注意,仅当可能的字段值(a)非常短且(b)永远不会包含逗号时,才建议使用此方法。因此它适用于复选框,但它可能不是处理文本输入字段的最佳方法,甚至不应考虑用于textareas。