嗨,
我正在创建以名称loan0
开头的动态输入文本框,并命名为:balance0
我想让经典的asp把值放到两个数组中
HTML:
<input type="text" name="name" value="John">
<input type="text" name="phone" value="1234567">
<input type="text" name="loan0" value="bla">
<input type="text" name="loan1" value="blabla">
<input type="text" name="balance0" value="test1">
<input type="text" name="balance1" value="test2">
...........and so on...............
经典ASP:
loan_array = bla,blabla ... and so on..
balance_array = test1,test2 ... and so on...
我尝试了什么:
dim loan_array
loan_array = ""
For Each item In Request.Form
loan_array = loan_array & Request.Form(item) & ","
Next
Response.write loan_array
end if
问题:我得到的结果是所有输入文本框中的所有值,但我只想要那些以名称开头后跟一个数字(以0开头)和贷款后跟一个数字(从0开始)
非常感谢任何帮助。
答案 0 :(得分:2)
为什么ASP可以为你做到这一点?
HTML:
<input type="text" name="name" value="John">
<input type="text" name="phone" value="1234567">
<input type="text" name="loan" value="bla">
<input type="text" name="loan" value="blabla">
<input type="text" name="balance" value="test1">
<input type="text" name="balance" value="test2">
<!-- ...........and so on............... -->
经典ASP:
Response.Write Request.Form("loan")
输出:
bla, blabla
ASP甚至会根据具有相同名称的表单元素的顺序构建分隔字符串。
基于feedback about handling commas inside the request,我自己做了一些测试,问题在于Request.Form("loan")
如何自动解码和整理条目,但有一个简单的方法,这是迭代Request.Form("loan")
的实例,而不是只调用Request.Form("loan")
并将它们自动整理在一起。
HTML:
<input type="text" name="loan" value="bla">
<input type="text" name="loan" value="blabla">
<input type="text" name="loan" value="test1,test2">
<input type="text" name="loan" value="test3">
<!-- ...........and so on............... -->
经典ASP:
Dim item
For Each item In Request.Form("loan")
Response.Write item & "<br />"
Next
输出:
bla
blabla
test1,test2
test3
答案 1 :(得分:1)
你应该这样做的方式是Lankymart所写的,即对所有相关字段使用相同的名称,并让ASP为你做串联到字符串。 (然后如果你想把它作为一个数组,你可以在逗号上做Split()
。)但是,如果你真的想自己做,你可以在Request.Form
中连接字段名:< / p>
N = Request.Form("N") '- number of fields per type
redim loan_array(N) : redim balance_array(N)
For i = 0 to N
loan_array(i) = Request.Form("loan" & i)
'- might as well take advantage of this one-at-a-time approach:
If Not IsNumeric(loan_array(i)) Then
loan_array(i) = 0
Else
loan_array(i) = CLng(loan_array(i))
End If
balance_array(i) = Request.Form("balance" & i)
'- etc.
Next
'- one reason for doing it this way might be that your data has commas in it:
Const delimiter = ";"
loan_list = Join(loan_array,delimiter)
balance_list = Join(balance_array,delimiter)
这依赖于每种类型的已知数量的字段,但是我无法想象在您生成/写出表单之后您不知道的情况 - 如果没有其他地方:
<input type="text" name="loan0" value="<%=loan_array(0)%>">
<input type="text" name="loan1" value="<%=loan_array(1)%>">
<input type="text" name="loan2" value="<%=loan_array(2)%>">
...
<input type="hidden" name="N" value="2">
答案 2 :(得分:-2)
也许这会有所帮助。
dim loan_array, myCounter
loan_array = ""
myCounter = 0
For Each item In Request.Form
loan_array = loan_array & Request.Form(item) & myCounter & ", "
myCounter = myCounter+1
Next
Response.write loan_array