100分求解决多选框数组问题 复选框初始值全部为0,Text控件初始值为0000。当触发onclick事件时判断,如果当前复选框被选中那么它的值救改为1,否则为0。同时修改Text控件对应的位。数据提交时读取Text的数据即可。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是第一个问题的回答,先给100分吧.<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>福利</title> <SCRIPT><!--var Insur_weals = 0;function application_onsubmit() {var len = select.Insur_weal.length;for(var i = 0; i < len; i++){ if(select.Insur_weal[i].checked) { Insur_weals = Insur_weals | (1 << i); }}application.Insur_weals.value = Insur_weals;}//--></SCRIPT></head> <body> <form name="select"> <table> <tr> <td width="133" height="24" align="right"> <div align="left"> <p align="right">福利(保险): </div> <td width="190" height="24"><input type="checkbox" name="Insur_weal" value="1">养老保险 <input type="checkbox" name="Insur_weal" value="1">医疗保险</td> <td width="178" align="right" height="24"><input type="checkbox" name="Insur_weal" value="1">工伤保险 <input type="checkbox" name="Insur_weal" value="1">失业保险</td> </table> </form> <form name="application" method="POST" action="http://localhost:1024/" onsubmit="return application_onsubmit()"> <p><input type="submit" value="提交"></p> <input type="hidden" name="Insur_weals"<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>福利</title> <SCRIPT><!--var Insur_weals = 0;function application_onsubmit() {var len = select.Insur_weal.length;for(var i = 0; i < len; i++){ if(select.Insur_weal[i].checked) { Insur_weals = Insur_weals | (1 << i); }}application.Insur_weals.value = Insur_weals;}//--></SCRIPT></head> <body> <form name="select"> <table> <tr> <td width="133" height="24" align="right"> <div align="left"> <p align="right">福利(保险): </div> <td width="190" height="24"><input type="checkbox" name="Insur_weal" value="1">养老保险 <input type="checkbox" name="Insur_weal" value="1">医疗保险</td> <td width="178" align="right" height="24"><input type="checkbox" name="Insur_weal" value="1">工伤保险 <input type="checkbox" name="Insur_weal" value="1">失业保险</td> </table> </form> <form name="application" method="POST" action="http://localhost:1024/" onsubmit="return application_onsubmit()"> <p><input type="submit" value="提交"></p> <input type="hidden" name="Insur_weals" value="0"> </form> </body></html> 这下是全部的答案了,注意那个application.Insur_weals.value是十进制值,它不可以接受二进制的值,使用时请注意。14是二进制的1110,所以第二,三,四个选项选中。好了,二百分,拿来吧,这几天正穷。<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>福利</title> <SCRIPT><!--var Insur_weals = 0;function application_onsubmit() {var len = select.Insur_weal.length;for(var i = 0; i < len; i++){ Insur_weals = 0; if(select.Insur_weal[i].checked) { Insur_weals = Insur_weals | (1 << i); }}application.Insur_weals.value = Insur_weals;}function initSelect(){ Insur_weals = application.Insur_weals.value; var len = select.Insur_weal.length; for(var i = 0; i < len; i++) { if(Insur_weals & (1<<i))select.Insur_weal[i].checked=true; }}//--></SCRIPT></head> <body onload="initSelect()"> <form name="select"> <table> <tr> <td width="133" height="24" align="right"> <div align="left"> <p align="right">福利(保险): </div> <td width="190" height="24"><input type="checkbox" name="Insur_weal" value="1">养老保险 <input type="checkbox" name="Insur_weal" value="1">医疗保险</td> <td width="178" align="right" height="24"><input type="checkbox" name="Insur_weal" value="1">工伤保险 <input type="checkbox" name="Insur_weal" value="1">失业保险</td> </table> </form> <form name="application" method="POST" action="http://localhost:1024/" onsubmit="return application_onsubmit()"> <p><input type="submit" value="提交"></p> <input type="hidden" name="Insur_weals" value="14"> </form> </body></html> Insur_weals = Insur_weals | (1 << i); ??? 一般在Post的Jsp或Servlet上进行取值:JSP或Html:<form name="testform" action="save.jsp" method="post"> //加入...<tr> <td width="133" height="24" align="right"> <div align="left"> <p align="right">福利(保险): </div> <td width="190" height="24"> <input type="checkbox" name="Insur_weal" value="1">养老保险 <input type="checkbox" name="Insur_weal" value="1">医疗保险 </td> <td width="178" align="right" height="24"> <input type="checkbox" name="Insur_weal" value="1">工伤保险 <input type="checkbox" name="Insur_weal" value="1">失业保险 </td> </tr> ...</from> //加入在save.jsp中:<%String sInsur_weal = "";String[] str=request.getParameterValues("Insur_weal");for (int i=0; i<str.length; i++) { if (str[i] == null) { sInsur_weal := sInsur_weal + "0"; } else { sInsur_weal := sInsur_weal + "1"; }}System.out.println(sInsur_weal); // 返回"0010"或"1110"等值//@todo: 存储sInsur_weal变量的值到数据库%> 我的第一次回答在粘贴源代码时出错了,第二次回答正确。Insur_weals = Insur_weals | (1 << i); 是位运算啊。 奇怪啊。for (int i=0; i<str.length; i++) { if (str[i] == null) { sInsur_weal = sInsur_weal + "0"; } else { sInsur_weal = sInsur_weal + "1"; }}只能够插入“1”直,而不能够插入“0”哦。 这是接收的一唯sInsur_weal的,要是二唯的呢?也就是说,如果有多个sInsur_weal要传递给save.jsp页面,又该怎么接收呢?还有,接收了如何拆分呢?谢谢,想到了问问! if (operType!=null&&operType.equals("2")) { String delID[]; delID = request.getParameterValues("sInsur_weal"); if (delID!=null) { ArrayList delArr = new ArrayList(); for (int j=0;j< delID.length;j++) { delArr.add(delID[j]); } } }这样就可以接受放在数组delID里面并且放入ArrayList 里面 为什么要是二维的放在值<input type="checkbox" name="Insur_weal" value="1,x">养老保险 逗号分割传上去就可以save页面上通过数组取值后再进行分割字符串 不能插入0是因为checkbox不将为选中的提交上去只提交选中的,这是简单的html的知识 stoneopen1(d) 我的方法(我的第二个回贴)是简单而且可行的,你怎么还……,还不???? Struts2中的“拦截器”为什么无法更改跳转页面? 救命呀~~~~ 如何在页面取select的值 请教JSP中的问题!!! 请求高手给思路 求一个批量生成卡的类 关于web.xml下Context标记中的debug属性 servlet怎么处理中文??? ZK怎么添加jsp?!! 现在java开发网站(applet)用什么工具阿? 想修改源程序,可不知道保存在什么地方的时候怎么办? 一个有关<jsp:useBean>标签中四种scope属性的问题,谢谢!!!马上送分!!! 怎么样声明一个动态数组?在线等!
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>福利</title>
<SCRIPT>
<!--
var Insur_weals = 0;
function application_onsubmit() {
var len = select.Insur_weal.length;
for(var i = 0; i < len; i++)
{
if(select.Insur_weal[i].checked)
{
Insur_weals = Insur_weals | (1 << i);
}
}
application.Insur_weals.value = Insur_weals;
}//-->
</SCRIPT>
</head>
<body>
<form name="select">
<table>
<tr>
<td width="133" height="24" align="right">
<div align="left">
<p align="right">福利(保险):
</div>
<td width="190" height="24"><input type="checkbox" name="Insur_weal" value="1">养老保险
<input type="checkbox" name="Insur_weal" value="1">医疗保险</td>
<td width="178" align="right" height="24"><input type="checkbox" name="Insur_weal" value="1">工伤保险
<input type="checkbox" name="Insur_weal" value="1">失业保险</td>
</table>
</form>
<form name="application" method="POST" action="http://localhost:1024/" onsubmit="return application_onsubmit()">
<p><input type="submit" value="提交"></p>
<input type="hidden" name="Insur_weals"<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>福利</title>
<SCRIPT>
<!--
var Insur_weals = 0;
function application_onsubmit() {
var len = select.Insur_weal.length;
for(var i = 0; i < len; i++)
{
if(select.Insur_weal[i].checked)
{
Insur_weals = Insur_weals | (1 << i);
}
}
application.Insur_weals.value = Insur_weals;
}//-->
</SCRIPT>
</head>
<body>
<form name="select">
<table>
<tr>
<td width="133" height="24" align="right">
<div align="left">
<p align="right">福利(保险):
</div>
<td width="190" height="24"><input type="checkbox" name="Insur_weal" value="1">养老保险
<input type="checkbox" name="Insur_weal" value="1">医疗保险</td>
<td width="178" align="right" height="24"><input type="checkbox" name="Insur_weal" value="1">工伤保险
<input type="checkbox" name="Insur_weal" value="1">失业保险</td>
</table>
</form>
<form name="application" method="POST" action="http://localhost:1024/" onsubmit="return application_onsubmit()">
<p><input type="submit" value="提交"></p>
<input type="hidden" name="Insur_weals" value="0">
</form>
</body>
</html>
好了,二百分,拿来吧,这几天正穷。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>福利</title>
<SCRIPT>
<!--
var Insur_weals = 0;
function application_onsubmit() {
var len = select.Insur_weal.length;
for(var i = 0; i < len; i++)
{
Insur_weals = 0;
if(select.Insur_weal[i].checked)
{
Insur_weals = Insur_weals | (1 << i);
}
}
application.Insur_weals.value = Insur_weals;
}
function initSelect()
{
Insur_weals = application.Insur_weals.value;
var len = select.Insur_weal.length;
for(var i = 0; i < len; i++)
{
if(Insur_weals & (1<<i))select.Insur_weal[i].checked=true;
}
}//-->
</SCRIPT>
</head>
<body onload="initSelect()">
<form name="select">
<table>
<tr>
<td width="133" height="24" align="right">
<div align="left">
<p align="right">福利(保险):
</div>
<td width="190" height="24"><input type="checkbox" name="Insur_weal" value="1">养老保险
<input type="checkbox" name="Insur_weal" value="1">医疗保险</td>
<td width="178" align="right" height="24"><input type="checkbox" name="Insur_weal" value="1">工伤保险
<input type="checkbox" name="Insur_weal" value="1">失业保险</td>
</table>
</form>
<form name="application" method="POST" action="http://localhost:1024/" onsubmit="return application_onsubmit()">
<p><input type="submit" value="提交"></p>
<input type="hidden" name="Insur_weals" value="14">
</form>
</body>
</html>
JSP或Html:
<form name="testform" action="save.jsp" method="post"> //加入
...
<tr>
<td width="133" height="24" align="right">
<div align="left">
<p align="right">福利(保险):
</div>
<td width="190" height="24">
<input type="checkbox" name="Insur_weal" value="1">养老保险
<input type="checkbox" name="Insur_weal" value="1">医疗保险
</td>
<td width="178" align="right" height="24">
<input type="checkbox" name="Insur_weal" value="1">工伤保险
<input type="checkbox" name="Insur_weal" value="1">失业保险
</td>
</tr>
...
</from> //加入在save.jsp中:
<%
String sInsur_weal = "";
String[] str=request.getParameterValues("Insur_weal");
for (int i=0; i<str.length; i++) {
if (str[i] == null) {
sInsur_weal := sInsur_weal + "0";
} else {
sInsur_weal := sInsur_weal + "1";
}
}
System.out.println(sInsur_weal); // 返回"0010"或"1110"等值
//@todo: 存储sInsur_weal变量的值到数据库
%>
Insur_weals = Insur_weals | (1 << i); 是位运算啊。
if (str[i] == null) {
sInsur_weal = sInsur_weal + "0";
} else {
sInsur_weal = sInsur_weal + "1";
}
}
只能够插入“1”直,而不能够插入“0”哦。
if (operType!=null&&operType.equals("2"))
{
String delID[];
delID = request.getParameterValues("sInsur_weal");
if (delID!=null)
{
ArrayList delArr = new ArrayList();
for (int j=0;j< delID.length;j++)
{
delArr.add(delID[j]);
}
} }
这样就可以接受放在数组delID里面
并且放入ArrayList 里面
<input type="checkbox" name="Insur_weal" value="1,x">养老保险
逗号分割传上去就可以
save页面上通过数组取值后
再进行分割字符串
checkbox不将为选中的提交上去
只提交选中的,这是简单的html的知识
我的方法(我的第二个回贴)是简单而且可行的,你怎么还……,还不????