设置选项以从数组中选择下拉列表。

Setting option to selected for drop down from an array.

我使用的是 Classic ASP,我有一个循环填充下拉框的数组。我试图做到这一点,所以当您提交表单时,它会保留所选的值。

<select name="ID" style="border-width: 1px; width: 260px;" >
                <%
                    For idx = 1 to Ubound (g_Array)
                    %>
                        <option value="<%response.Write(idx)%>"
                        <% If Request.Form("ID") = idx Then Response.Write("selected") %>> <%response.write(g_Array(idx))%>
                        </option>
                    <%
                    next
                %>
            </select>

尝试使用 cint。默认情况下,查询字符串和表单变量被视为字符串,您需要告诉 ASP 将它们视为整数。

<select name="ID" style="border-width: 1px; width: 260px;" >


<%
                For idx = 1 to Ubound (g_Array)
                %>
                    <option value="<%=idx%>"
                    <% If cint(Request.Form("ID")) = idx Then Response.Write("selected") %>> <%=g_Array(idx)%>
                    </option>
                <%
                next
            %>
        </select>

请注意,您可以使用 <%=idx%> 作为 <%response.Write(idx)%>

的更短替代方案