2011-06-09 82 views
1

使用会话变量来存储复选框的选中状态时出现问题。我正在使用分页,因此当每个字母被按下时,一个复选框将出现,并将相应的字母存储为其值。当一个复选框被选中时,它的状态被保存,但问题是当我取消选中复选框时,它仍然被选中。也不知道这是否相关,但我已经改变了按钮看起来像超链接,所以我可以使用post方法,而不是使用查询字符串,因为我不想使用它。下面提供的代码会话变量经典asp

<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;">&nbsp; 
<% 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 

回答

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;">&nbsp; 
     <% 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

我会做的是使用一个隐藏字段保存最后一个字母

 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%>" />