读书人

checkbox与vector传送有关问题

发布时间: 2012-01-07 21:41:55 作者: rapoo

checkbox与vector传送问题
通过checked判断数值加入到vector中传递,为什么t2.jsp收到为空值??请帮忙看看有什么问题,谢谢!

t1.jsp
<%@ page contentType= "text/html; charset=gb2312 " language= "java " import= "java.sql.*,java.util.* " errorPage= " " %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 " />
<title> 测试数组传送 </title>
</head>

<body>
<form id= "form1 " name= "form1 " method= "post " action= "t2.jsp ">
<input name= "test1 " type= "checkbox " value= "value1 " checked= "checked " /> 测试1 <br />
<input type= "checkbox " name= "test2 " value= "value2 " /> 测试2 <br />
<input type= "checkbox " name= "test3 " value= "value3 " /> 测试3 <br />
<input type= "checkbox " name= "test4 " value= "value4 " /> 测试4 <br />
<input type= "submit " name= "Submit " value= "提交 ">
</form>
</body>
</html>
<% Vector test = new Vector();
// Vector buyList=(Vector)session.getValues( " ", " ");
//document.form1.test.checked ;
%>
<script>
if ( document.form1.test1.checked == true )
{ <% test.add( "value1 "); %> ;}else
{ <% test.remove( "value1 "); %> ;};
if ( document.form1.test2.checked == true )
{ <% test.add( "value2 "); %> ;}else
{ <% test.remove( "value2 "); %> ;};
if ( document.form1.test3.checked == true )
{ <% test.add( "value3 "); %> ;}else
{ <% test.remove( "value3 "); %> ;};
if ( document.form1.test4.checked == true )
{ <% test.add( "value4 "); %> ;}else
{ <% test.remove( "value4 "); %> ;};
</script>
<%
session.setAttribute( "test ",test);
%>


t2.jsp
<%@ page contentType= "text/html; charset=gb2312 " language= "java " import= "java.sql.*,java.util.* " errorPage= " " %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml ">


<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 " />
<title> 测试数组传送接收 </title>
</head>
<%
Vector test = (Vector)session.getAttribute( "test ");
for(int i=0;i <test.size();i++)
{
String str=(String)test.elementAt(i);
out.print(str);
}
%>

[解决办法]
<script>
if ( document.form1.test1.checked == true )
{ <% test.add( "value1 "); %> ;}else
{ <% test.remove( "value1 "); %> ;};
if ( document.form1.test2.checked == true )
{ <% test.add( "value2 "); %> ;}else
{ <% test.remove( "value2 "); %> ;};
if ( document.form1.test3.checked == true )
{ <% test.add( "value3 "); %> ;}else
{ <% test.remove( "value3 "); %> ;};
if ( document.form1.test4.checked == true )
{ <% test.add( "value4 "); %> ;}else
{ <% test.remove( "value4 "); %> ;};
</script>
<%
session.setAttribute( "test ",test);
%> 做什么用的呢????????这样写是不对的.

不知道你的目的是什么哦



[解决办法]
如果你只是想在第二页获取第一页点的框,

1,在jsp2中,request.getParameter( "xxx ");
2,jsp的checkbox都用一个名字test,在jsp2中request.getParameterValues( "test ")返回一个string[];

如果你要在第一页点了就加到sesssion的Vector中,要用的异步传输,就是常说的ajax了.

读书人网 >Java Web开发

热点推荐