js选择checkbox

<pre name="code" class="html"><p>
<input type="checkbox" name="love" onclick="selectAllNo()">全选/全不选</p>
<p>
<input type="checkbox" name="love">篮球</p>
<p>
<input type="checkbox" name="love">足球</p>
<p>
<input type="checkbox" name="love">乒乓球</p>

<input type="button" value="全选" onclick="selectAll()">
<input type="button" value="全不选" onclick="selectNo()">
<input type="button" value="反选" onclick="reserve()">

	var check=document.getElementsByName("love");


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

	function selectAll () {
		for (var i = 0; i < check.length; i++) {
			check[i].checked=true;
		};
	}
	function selectNo () {
		for (var i = 0; i < check.length; i++) {
			check[i].checked=false;
		};
	}
	function reserve () {
		for (var i = 0; i < check.length; i++) {
			check[i].checked=(check[i].checked==true)?false:true;
		};
	}

	function selectAllNo () {
		if(check[0].checked==true)
		{
			selectAll();
		}
		else
		{	
			selectNo();
		}
	}




posted @ 2015-09-14 21:17  Fighter,  阅读(151)  评论(0编辑  收藏  举报