从action传到页面一个数组,然后用这个数组里的值控制页面里一组checkbox默认被选中
如action出过去 a , b , cpublic 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中传来的数组
如action出过去 a , b , cpublic 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中传来的数组
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>
value="${db}"
<!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");
}实验了。 看下吧