javascript select all (checkbox) function writing! ! ! ! !

	<form action="${pageContext.request.contextPath}/employ/list"
		id="myform" method="get">
		<div id="formDiv">
			<table id="tb1">
				<tr>
					<th>姓名<input id="eName" name="eName"></th>
					<th>编号<input id="deptNo" name="deptNo"></th>
					<td><input type="submit" value="查询"></input></td>

					<td><input type="button" value="新建" onclick="swithadd()"></td>
				</tr>
			</table>
			<table id="tb2" border="1px">
				<tr>
					<th><input type="checkbox" name ="cbxall" onclick="javascript:selAll(this);"></th>
					<th>Name</th>
					<th>work</th>
					<th>birthday</th>
					<th>Wage</th>
					<th>Department ID</th>
				</tr>
				<tbody>
					<c:forEach items="${list}" var="emp">
						<tr>
							<td><input type="checkbox" name ="cbxall"></td>
							<td>${emp.eName}</td>
							<td>${emp.eJob}</td>

							<td>${emp.eBirthday}</td>
							<td>${emp.eSalary}</td>
							<td>${emp.deptNo}</td>
							<td><input type="button" value="删除"
								onclick="deleteEmp(${emp.empNo})"></td>
							<td><input type="button" value="修改"
								onclick="updateEmp(${emp.empNo})"></td>
						</tr>
					</c:forEach>
				</tbody>
			</table>
		</div>
	</form>
<script language='javascript'>
//select all
	function selAll(turnobj){
		
		try{
		if(turnobj.checked){
			var obj=document.forms[0].cbxall;
			for(i=0;i<obj.length;i++){
				if(!obj[i].checked&&!obj[i].disabled){
					obj[i].checked=true;
				}
			}
		}else{
			var obj=document.forms[0].cbxall;
			for(i=0;i<obj.length;i++){
				if(obj[i].checked){
					obj[i].checked=false;
				}
			}
		}
		}catch(e){
			
		}
	}

</script>

 The above HTML is my form. You can write it according to different situations. The following function is a general js method. In the loop, you can modify the obj.length value of i<obj.length according to the situation. All the queried values ​​are selected. There are drawbacks. This value can be equal to the number of displayed items per page during paging, so that the number of items per page can not be selected, and the data on the following pages will not be selected! ! ! ! ! !

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326581684&siteId=291194637