1. 程式人生 > >使用jQuery完成複選框的全選和全不選

使用jQuery完成複選框的全選和全不選

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		
		<script type="text/javascript" src="../js/jquery-1.8.3.js" ></script>
		<script>
			$(function(){
				$("#cb1").click(function(){
//		1.			$("tbody input").attr("checked",this.checked);
//   	2.
					$("tbody input").prop("checked",this.checked);
				});
			});
		</script>
	</head>
	<body>
		<table border="1px"style="margin: auto;">
			<tr>
				<th>
					<input type="checkbox" id="cb1"/>
				</th>
				<th>id</th>
				<th>name</th>
				<th>age</th>
			</tr>
			<tr>
				<td>
					<input type="checkbox" name="checkbox1"/>
				</td>
				<td>1</td>
				<td>tom</td>
				<td>21</td>
			</tr>
			<tr>
				<td>
					<input type="checkbox" name="checkbox1"/>
				</td>
				<td>2</td>
				<td>jack</td>
				<td>22</td>
			</tr>
			<tr>
				<td>
					<input type="checkbox" name="checkbox1"/>
				</td>
				<td>3</td>
				<td>lucy</td>
				<td>23</td>
			</tr>
		</table>
	</body>
</html>