js中checkbox反选

时间:2023-03-08 22:10:25
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script>
window.onload = function (){
	var aInp = document.getElementsByTagName('input');

	// aInp[1].checked = false;
	// aInp[2].checked = true;

	aInp[0].onclick = function (){
		for( var i=1; i<aInp.length; i++ ){
			aInp[i].checked = !aInp[i].checked;
			/*
			if( aInp[i].checked ) {
				aInp[i].checked = false;
			} else {
				aInp[i].checked = true;
			}
			*/
		}
	};
};
</script>
</head>

<body>

<input type="button" value="反选" />
<ul>
	<li><input type="checkbox" checked /></li>
	<li><input type="checkbox" /></li>
	<li><input type="checkbox" checked /></li>
	<li><input type="checkbox" /></li>
	<li><input type="checkbox" /></li>
</ul>

</body>
</html>