我使用的是经典的 asp,我有一个用户选择的下拉列表,然后按下提交。在他们按下提交后,下拉列表将返回默认值而不是他们选择的值。无论如何要保持回发之间的下拉状态而不是回到默认状态?如果需要,可以发布代码示例。
谢谢!
最佳答案
您必须根据用户发布的值在服务器端“选择它”。
<select id="cars">
<option value="volvo"
<%
if request.form("cars") = "volvo" then
response.write("selected")
end if %>
>Volvo</option>
<option value="Saab"
<%
if request.form("cars") = "Saab" then
response.write("selected")
end if %>
>Saab</option>
<option value="Mercedes"
<%
if request.form("cars") = "Mercedes" then
response.write("selected")
end if %>
>Mercedes</option>
<option value="Audi" <%
if request.form("cars") = "Audi" then
response.write("selected")
end if %>
>Audi</option>
</select>
当然,您可能希望自己开发自己的函数以避免所有这些样板文件。
<%
sub option(value, data, select_id)
Response.Write("<option value=""" & value & """)
if request.form(select_id) = value then
Response.Write("selected")
end if
Response.Write(">" & data & "</option>")
end sub
%>
' (...)
<select id="cars">
<% option("volvo", "Volvo", "cars") %>
<% option("Saab", "Saab", "cars") %>
<% option("Mercedes", "Mercedes", "cars") %>
<% option("Audi", "Audi", "cars") %>
</select>
如果您向函数传递一个空白的
select_id
,它就不会在意回发时尝试选择 select
的选定项目。关于asp-classic - 回发后下拉不保留选定的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2487783/