`
chourentang
  • 浏览: 56696 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Jquery实现checkbox全选

阅读更多
1.方式一
<script type="text/javascript">
	$(function() {
	    $("#cc").click(function() {
		$("input[@name='c']").each(function() {
		    $(this).attr("checked", true);
		});
	    });
	    $("#cc").click(function() {
		$("input[@name='c']").each(function() {
		    $(this).attr("checked", false);
		});
	    });
	});
</script>

<input type='checkbox' id='c' name='c' value='10' />UserA
<input type='checkbox' id='c' name='c' value='11' />UserB
<input type='checkbox' id='c' name='c' value='12' />UserC

<input type="button" id='c' name=" id="cc" value="全选/取消" />


2.方式二
$("#cc").click(
	function(){
	    if(this.checked){
		$("input[name='c']").each(function(){this.checked=true;});
	    }else{
		$("input[name='c']").each(function(){this.checked=false;});
	    }
	}
);

<input type='checkbox' id='c' name='c' value='10' />UserA
<input type='checkbox' id='c' name='c' value='11' />UserB
<input type='checkbox' id='c' name='c' value='12' />UserC

<input type="button" id='c' name="cc" id="cc" value="全选/取消" />


2.方式三
<script type="text/javascript">
	function checkAll(){
		var check_del = document.getElementsByName("c");
		var check_delAll = document.getElementById("cc");
		if(check_delAll.checked) {
			alert(cc.length);
			for(var m=0; m < check_del.length; m++) {
				check_del[m].checked = true;
			}
		}else{
			for(var m=0; m < check_del.length; m++) {
				check_del[m].checked = false;
			}
		}
	}
</script>

<input type='checkbox' id='c' name='c' value='10' />UserA
<input type='checkbox' id='c' name='c' value='11' />UserB
<input type='checkbox' id='c' name='c' value='12' />UserC

<input type="button" id='c' name="cc" id="cc" value="全选/取消" />
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics