为什么这个经典的ASP下拉菜单没有选择正确的值?

时间:2015-03-22 22:00:09

标签: html asp-classic

循环中的前几个下拉列表将正常工作(这是从SQL获取的3个测试记录)。为什么选择第3个值的列表中的最后一个值,然后继续为所有空的clientnos()值选择最后一个值?

<%For k = 0 to 4%>
<td colspan ="5"><select name = "clientnos(<%=k%>)" id="aliasInput" >
<option value=""></option>

<%
strSQL1 = "select cno, aliasname from Clients order by Aliasname asc"
Set rs1 = objConnection.Execute(strSQL1, ,adCmdText)
call ErrorHandler(err)
arr1 = rs1.GetRows()

for i = 0 to UBound(arr1,2)

    cno = trim(arr1(0,i))
    aliasname = trim(arr1(1,i))

%>

<%if cno <> clientnos(k) Then%><option value = "<%=cno%>"><%=aliasname%></option><%end if%>
<%if cno = clientnos(k) Then%><option value = "<%=cno%>" selected><%=aliasname%></option><%end if%>
<%
next

%>
</select></td>
<%next%>

1 个答案:

答案 0 :(得分:0)

我通过最初默认为空白选择来修正错误选择,并发现下拉列表的初始值导致问题,即下拉列表中的最后一条记录不正确。这是更正后的代码。

<td colspan ="5"><select name = "selectedclientnos(k)" id="aliasInput" >
<option value="" selected></option>
<%
strSQL1 = "select cno, aliasname from Clients order by Aliasname asc"
Set rs1 = objConnection.Execute(strSQL1, ,adCmdText)
call ErrorHandler(err)
arr1 = rs1.GetRows()

for i = 0 to UBound(arr1,2)

    cno = trim(arr1(0,i))
    aliasname = trim(arr1(1,i))

%>

<%if cno <> clientnos(k) Then%><option value = "<%=cno%>"><%=aliasname%></option><%end if%>
<%if cno = clientnos(k) Then%><option value = "<%=cno%>" selected><%=aliasname%></option><%end if%>
<%
next

%>