日期:2014-05-16  浏览次数:20432 次

js中控制checkbox的选择以及table的显示与隐藏
<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
	function checkAll() {
		var m = document.getElementsByName("all")[0];
		var s = document.getElementsByName("check");

		if (m.checked) {

			for ( var i = 0; i < s.length; i++) {
				s[i].checked = true;
			}
		} else {
			for ( var i = 0; i < s.length; i++) {
				s[i].checked = false;
			}

		}
	}

	function checkEvery() {
		var m = document.getElementsByName("all")[0];
		var s = document.getElementsByName("check");

		for ( var i = 0; i < s.length; i++) {
			if (s[i].checked&&m.checked) {
				m.checked = true;
			} else {
				m.checked = false;
			}
		}

	}

	function turn() {
		with (document) {
			var m = getElementById("change");
			var n = getElementById("table");
			if (m.value == "收缩") {
				n.style.display = "none";
				m.value = "展开";
			} else {
				n.style.display = "block";
				m.value = "收缩";
			}
		}
	}
</script>
</head>
<body>
	<table border="1" align="center" width="60%" id="table1">
		<tr>
			<td><input type="checkbox" name="all" onclick="checkAll();">全选</td>
			<td><input type="button" value="收缩" id="change"
				onclick="turn();"></td>
		</tr>
	</table>
	<%
		int number = Integer.parseInt(request.getParameter("number"));
	%>

	<table border="1" align="center" width="60%" id="table">
		<%
			for (int i = 0; i < number; i++) {
		%>
		<tr>
			<td><input type="checkbox" name="check" onclick="checkEvery()"></td>
			<td><%=i%></td>
		</tr>
		<%
			}
		%>
	</table>
</body>
</html>