回发后下拉不保留所选值 [英] drop down not retaining selected value after post back
问题描述
我使用的是经典的 asp,我有一个用户选择然后按提交的下拉列表.在他们按下提交后,下拉列表将返回默认值,而不是他们选择的值.无论如何要保持回发之间的下拉状态而不是回到默认状态?如果需要,可以发布代码示例.谢谢!
I'm using classic asp, I have a drop down list that the user selects and then presses submit. After they press submit the drop down list is going back to the default value instead of what they selected. Is there anyway to keep the state of the drop down between post backs instead of it going back to the default? Can post code sample if needed. Thanks!
推荐答案
你必须根据用户发布的值选择它"服务器端.
You have to "select it" serverside according to the values that the user has POSTed.
<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>
当然,您可能希望自行开发自己的函数以避免所有这些样板.
Of course, you might want to homegrown your own function to avoid all that boilerplate.
<%
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
的选中项.
If you pass the function a blank select_id
, it will not care about trying to select the selected item of the select
on postback.
这篇关于回发后下拉不保留所选值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!