1. 仅仅利用javascript进行操作:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 |
//html代码如下:<form action="#"
method="POST"
onsubmit="return form1();"><div> <input type="checkbox"
id="kee" name="ke[]"
value="1">11111 <input type="checkbox"
id="kee" name="ke[]"
value="2">22222 <input type="checkbox"
id="kee" name="ke[]"
value="3">33333 <input type="checkbox"
id="kee" name="ke[]"
value="4">44444 <input type="checkbox"
id="kee" name="ke[]"
value="5">55555 <input type="checkbox"
id="kee" name="ke[]"
value="6">66666 <input type="checkbox"
id="kee" name="ke[]"
value="7">77777 <input type="checkbox"
id="kee" name="ke[]"
value="8">88888 <input type="checkbox"
id="kee" name="ke[]"
value="9">99999</div><div><input type="submit"
value="发布"></div></form>//javascript的demo示例<script type="text/javascript">function form1(){ var x=document.getElementsByName("ke[]"); var m=0; var n=false; for(var i=0;i<x.length;i++) { if(x[i].checked) { n=true; m++; } } if(!n) { alert("至少选择一个啊"); } if(m>5) { alert("选这么多干吗"); }}</script> |
2.在后台jsp中进行操作
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 |
1.html输入参数页面<html> <head> </head> <body> <form action="02.jsp"
method="post"> 姓名:<input type="text"
name="uname"
/> <p> 擅长技术: <input type="checkbox"
name="tech"
value="J2EE"
/>J2EE <input type="checkbox"
name="tech"
value=".NET"
/>.NET <input type="checkbox"
name="tech"
value="ASP"
/>ASP <input type="checkbox"
name="tech"
value="PHP"
/>PHP <p> <input type="submit"
value="提交"
/> </form> </body></html>2. 后台jsp参数处理<%@page
contentType="text/html;charset=GB2312"
%><html> <head> <title>02.jsp</title> </head> <body> <% request.setCharacterEncoding("GB2312"); String Name=request.getParameter("uname"); //获得参数数组 String Tech[]=request.getParameterValues("tech"); %> <h1>姓名:<%=Name %></h1> <h1>擅长技术: <% //输出数组 int
i; for(i=0;i<Tech.length;i++) { %> <%=Tech[i] %> <% } %> </h1> </body></html> |
页面中checkbox返回的是一个数组,如何对数组进行操作,布布扣,bubuko.com
原文:http://www.cnblogs.com/haore147/p/3617956.html