【www.bbyears.com--js教程】
从JSP页面获取复选框的values值,通过定义数组,将其值存入数组之中,并显示出来
最简单的就是
代码如下
String strLove = "";
String[] strLoves = (String[])request.getParameterValues("love");
//通过循环读取每个选中项
for (String love : strLoves)
{
strLove = strLove + love + ",";
}
strLove = strLove.substring(0,strLove.length()-1);
例
html页面
代码如下jsp处理页面
代码如下<%@page contentType="text/html;charset=GB2312" %>
<%
request.setCharacterEncoding("GB2312");
String Name=request.getParameter("uname");
//获得参数数组
String Tech[]=request.getParameterValues("tech");
%>
姓名:<%=Name %>
擅长技术:
<%
//输出数组
int i;
for(i=0;i
{
%>
<%=Tech[i] %>
<%
}
%>
上面的代码不分中英文的,如果选项的值中含有中文,在JSP中的处理代码如下:
代码如下<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%!
public String chinese(String sText)
{
try
{
return new String(sText.getBytes("iso-8859-1"),"gbk");
}
catch(Exception e)
{
return sText;
}
}
%>
<%
String strLove = "";
String[] strLoves = (String[])request.getParameterValues("love");
//通过循环读取每个选中项
for (String love : strLoves)
{
strLove = strLove + chinese(love) + ",";
}
strLove = strLove.substring(0,strLove.length()-1);
%>
jsp 获得checkbox值并写入数据库,可通过以下方法:
代码如下
保存页面jsp程序
代码如下<%@ page contentType="text/html; charset=utf-8" language="java" import = "java.util.*, java.text.*,java.sql.*"%>
<%
if(menu.equals("add"))
{
String tmp="";
String SCorpMode="";
String[] CorpMode = request.getParameterValues("CorpMode");
if(CorpMode.length>0)
{
for(int i=0;i
SCorpMode=SCorpMode+CorpMode+",";
}
SCorpMode=SCorpMode.substring(0,SCorpMode.length()-1);//去掉SID中的最后一个逗号
}
tmp ="insert into Corporation (CorpMode) values("+SCorpMode+");
if(dbc.executeUpdate(tmp)>=0)
out.println("<script>alert("信息添加成功!");location.href="CorpAdd.jsp"</script>");
else
//out.println("<script>alert("添加失败!");location.href="CorpAdd.jsp"</script>");
out.print(tmp);
}
}
%>