使用会话变量存储复选框的选中状态时出现问题。我正在使用分页,因此当按下每个字母时,将出现一个复选框,其中相应的字母存储为其值。选中复选框时,会保存其状态,但问题是当我取消选中该复选框时,它会保持选中状态。也不知道这是否相关,但我已经将按钮更改为超链接,因此我可以使用post方法而不是使用查询字符串,因为我不想使用它。代码提供如下
<form action="Table.asp" method="post" name="form2">
<input type="submit" name="Button" value="#" style="background:transparent;border:0;display:inline;color:#00F;text-decoration:underline;padding:0px;cursor:pointer">
<% for i = 97 to 122 %>
<input type="submit" name="Button" value="<%=CHR(i) %>" style="background:transparent;border:0;display:inline;color:#00F;text-decoration:underline;padding:0px;cursor:pointer;">
<% next %>
</br></br></br>
<%
alphaB = request.form("Button")
if alphaB <>"" then
alphaCheck = request.form("checkBox")
if alphaCheck <>"" then
session("checkBox_"&alphaCheck) = "checked"
else
session("checkBox_"&alphaCheck) = ""
end if
%>
<input type="checkbox" name="checkBox" value="<%=alphaB %>" <%=session("checkBox_"&alphaB) %>>
<%
response.write alphaB
end if
答案 0 :(得分:3)
您应该在回发之前保存未经检查的值,然后使用此值。
<form action="Table.asp" method="post" name="form2">
<input type="submit" name="Button" value="#" style="background:transparent;border:0;display:inline;color:#00F;text-decoration:underline;padding:0px;cursor:pointer">
<% for i = 97 to 122 %>
<input type="submit" name="Button" value="<%=CHR(i) %>" style="background:transparent;border:0;display:inline;color:#00F;text-decoration:underline;padding:0px;cursor:pointer;">
<% next %>
</br></br></br>
<%
alphaB = request.form("Button")
if alphaB <>"" then
alphaCheck = request.form("checkBox")
if alphaCheck <>"" then
session("checkBox_"&alphaCheck) = "checked"
else
'EDIT use last one
session("checkBox_"&session("lastOne")) = ""
end if
'EDIT save the last one in session
session("lastOne") = alphaB
%>
<input type="checkbox" name="checkBox" value="<%=alphaB %>" <%=session("checkBox_"&alphaB) %>>
<%
response.write alphaB
end if
%>
答案 1 :(得分:1)
我要做的是使用隐藏字段来保存最后一个字母
hidAlphaCheck = request.form("lastcheckbox")
alphaCheck = request.form("checkBox")
if alphaCheck <>"" then
session("checkBox_"&hidAlphaCheck) = "checked"
else
session("checkBox_"&hidAlphaCheck) = ""
end if
...
<input type="checkbox" name="checkBox" value="<%=alphaB %>" <%=session("checkBox_"&alphaB) %>>
<input type="hidden" name="lastcheckbox" id="lastcheckbox" value="<%=alphaB%>" />