action传数组到jsp中,checkbox默认被选中,这个怎么做?

www_shenrenxue_com 2009-12-11 09:21:02
从action传到页面一个数组,然后用这个数组里的值控制页面里一组checkbox默认被选中
如action出过去 a , b , c

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
String[] arr = { "a", "b", "c" };
request.setAttribute("arr", arr);
return new ActionForward("/index.jsp");
}

index.jsp页面中有几个checkbox
<input type="checkbox" name="cb1" value="a">a<br>
<input type="checkbox" name="cb1" value="b">b<br>
<input type="checkbox" name="cb1" value="c">c<br>
<input type="checkbox" name="cb1" value="d">d<br>
...

我希望用Javascript方法,默认选中前三组,但我现在不知道怎么在jsp页面中接收action中传来的数组
...全文
665 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
helanye 2009-12-12
  • 打赏
  • 举报
回复
关注
malujun666 2009-12-12
  • 打赏
  • 举报
回复
还是用struts2好点
malujun666 2009-12-12
  • 打赏
  • 举报
回复
input标签里嵌套一个if标签来控制input标签的checked属性有么有
yonghenghxq 2009-12-12
  • 打赏
  • 举报
回复
用struts中的标签很容易实现
duanzongfen 2009-12-12
  • 打赏
  • 举报
回复
兄弟你可以直接用struts框架就可以了!!!
a513264420 2009-12-12
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 andy861025 的回复:]
Java code

先把数组传到JSP中,然后在JAVASCRIPT中处理.在<body onLoad="">中加载处理的方法.不知道适用不?
[/Quote]

这个方法可行的
jisi772864447 2009-12-11
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 andy861025 的回复:]
Java code

先把数组传到JSP中,然后在JAVASCRIPT中处理.在<body onLoad="">中加载处理的方法.不知道适用不?
[/Quote]

有用
jisi772864447 2009-12-11
  • 打赏
  • 举报
回复
先洗澡去了,,,嘿嘿
365810247 2009-12-11
  • 打赏
  • 举报
回复


先把数组传到JSP中,然后在JAVASCRIPT中处理.在<body onLoad="">中加载处理的方法.不知道适用不?

jisi772864447 2009-12-11
  • 打赏
  • 举报
回复
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
www_shenrenxue_com 2009-12-11
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 jisi772864447 的回复:]
页面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href=" <%=basePath%>">
   
    <title>My JSP 'demo.jsp' starting page </title>
<script type="text/javascript">
function test(){
var checkBoxs = document.getElementsByName("cb1");
<c:forEach items="${arr}" var="wangjian">
for(var i=0;i <checkBoxs.length;i++){
if(checkBoxs[i].value=='${wangjian}'){
checkBoxs[i].checked=true;
}
}
  </c:forEach>
}
window.onload=function(){
test();
}
</script>

  </head>
 
  <body>

后台

public ActionForward demo(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String[] arr = { "a", "b", "c" };
request.setAttribute("arr", arr);
return mapping.findForward("test");
}

实验了。 看下吧
[/Quote]
<c:forEach items="${arr}" var="wangjian">这个标签想使用,在jsp页面中要引入什么东西?
jisi772864447 2009-12-11
  • 打赏
  • 举报
回复
页面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'demo.jsp' starting page</title>
<script type="text/javascript">
function test(){
var checkBoxs = document.getElementsByName("cb1");
<c:forEach items="${arr}" var="wangjian">
for(var i=0;i<checkBoxs.length;i++){
if(checkBoxs[i].value=='${wangjian}'){
checkBoxs[i].checked=true;
}
}
</c:forEach>
}
window.onload=function(){
test();
}
</script>

</head>

<body>

后台

public ActionForward demo(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String[] arr = { "a", "b", "c" };
request.setAttribute("arr", arr);
return mapping.findForward("test");
}

实验了。 看下吧
howsun_zh 2009-12-11
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zhangjihao 的回复:]
<c:forEachitems="${arrDb}" var="db"><inputtype="checkbox" name="cb1" value="db" <c:if test='${fn:contains(arr,db)}'>checked="true"</c:if>/>${db}<br/></c:forEach>
[/Quote]


少写了$:
value="${db}"
howsun_zh 2009-12-11
  • 打赏
  • 举报
回复
public ActionForward execute(ActionMapping mapping, ActionForm form, 
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
String[] arr = { "a", "b", "c" };
request.setAttribute("arr", arr);

String[] arrDb = { "a", "b", "c", "d" }; //注意元数据,例如从数据库取出来
request.setAttribute("arrDb", arrDb);

return new ActionForward("/index.jsp");
}


<c:forEach items="${arrDb}" var="db">
<input type="checkbox" name="cb1" value="db" <c:if test='${fn:contains(arr,db)}'>checked="true" </c:if>/>${db} <br />
</c:forEach>
www_shenrenxue_com 2009-12-11
  • 打赏
  • 举报
回复
没人回帖,我自己回,无奈啊,╮(╯▽╰)╭
www_shenrenxue_com 2009-12-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 houxq1126 的回复:]
我也不会啊啊啊啊
[/Quote]
等高手回了帖,我们就都会了。O(∩_∩)O哈哈~
www_shenrenxue_com 2009-12-11
  • 打赏
  • 举报
回复
回自己贴子,增加点人气,吸引高手来帮忙,╮(╯▽╰)╭
houxq1126 2009-12-11
  • 打赏
  • 举报
回复
我也不会啊啊啊啊
www_shenrenxue_com 2009-12-11
  • 打赏
  • 举报
回复
帖子不能沉,解决了再沉。
www_shenrenxue_com 2009-12-11
  • 打赏
  • 举报
回复
怎么没高手指教啊。。。。。。。。。。。。。。。。。。。。。
加载更多回复(1)

81,092

社区成员

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

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