关于checkbox返回值的问题

zh430 2004-09-07 03:14:32
下面这段写在servlet中的,目的是想取出jsp中的checkbox(即"sel")的值,不过输出的值只有选中的checkbox的value,没选中的就没有返回值了,因为在jsp中的checkbox是根据数据库的内容生成的,个数不定,我要在servlet中循环逐个判定是否被选中了,要怎么做呢

String[] role1 = (String[]) request.getParameterValues("sel");

if (role1 != null) {
for (int i = 0; i < role1.length; i++) {
String role = role1[i];
System.out.println("role:" + role);
}

}
...全文
473 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zh430 2004-09-08
  • 打赏
  • 举报
回复
如果在前台做,那地址后面不是要跟一长串的字串,也不安全啊
Ronanljy 2004-09-08
  • 打赏
  • 举报
回复
对,前台做。
Little_qd 2004-09-07
  • 打赏
  • 举报
回复
String ids[] = request.getParameterValues("id") ;
if(ids != null) {
for(int i=0;i<ids.length;i++) {
System.out.println(ids[i]);
}
}
makay 2004-09-07
  • 打赏
  • 举报
回复
用getParameterNames()吧。
如:
Enumeration enum=request.getParameterNames();
while(enum.hasMoreElements()){
String key=(String)enum.nextElement();
String value=request.getParameter(key);
out.println("<P>");
out.println(key+"="+value);
out.println("</P>");
}
tanyun1111 2004-09-07
  • 打赏
  • 举报
回复
支持楼上的
LxcJie 2004-09-07
  • 打赏
  • 举报
回复
这个可以在前台做,提交前组串,如下:

<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
index
</title>
<script language="javascript">
function subForm()
{
var arr = document.myForm.chk;
var stat = "";
for(var i=0; i<arr.length; i++)
{
if(arr[i].checked == true)
stat += "1";
else
stat += "0";
}
alert(stat);
document.myForm.action = "/webapp/test?statStr="+stat;
document.myForm.submit();
}
</script>
</head>
<body bgcolor="#ffffff">
<form name="myForm" action="/webapp/test" method="POST">
<input type="checkbox" name="chk" value="one"/>
<input type="checkbox" name="chk" value="two"/>
<input type="checkbox" name="chk" value="three"/>
<input type="checkbox" name="chk" value="four"/>
<input type="checkbox" name="chk" value="five"/>
<input type="button" onclick="subForm()" value="submit" />
</form>
</body>
</html>
zh430 2004-09-07
  • 打赏
  • 举报
回复
是啊,我要将所有的状态在后台组合成字串
LxcJie 2004-09-07
  • 打赏
  • 举报
回复
你需要的不就是选中的吗?你想干什么呢,把所有的都去到后台吗

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧