javascript - 如何让全选选中
本文介绍了javascript - 如何让全选选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
sel-all里的1、2、3、4、5、6全部选中时,全选框selectAll也显示选中。
求大神们不要给负分啊啊啊··我只是个菜鸟`请输入代码
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Checkbox Demo</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
</head>
<body>
<div class="sel-all">
<input type="checkbox" class="check-sel"/>1
<input type="checkbox" class="check-sel" />2
<input type="checkbox" class="check-sel"/>3
<input type="checkbox" class="check-sel" />4
<input type="checkbox" class="check-sel"/>5
<input type="checkbox" class="check-sel" />6
</div>
<input type="checkbox" id="selectAll" />全选
</body>
</html>
<script type="text/javascript">
$(function(){
$('#selectAll').click(function(){
$('.check-sel').attr('checked', $(this).attr('checked'));
});
$('.check-sel').click(function(){
var selnum=$('.sel-all').find('.check-sel');
if(selnum.attr('checked'))
{
$('.sel-all .check-sel').each(function(){
console.log(selnum)
$("#selectAll").attr('checked',true);
});
}
else{
$("#selectAll").removeAttr('checked');
}
});
});
</script>
解决方案
用的jQuery版本够老的啊
$(function () {
var $selectAll = $('#selectAll');
var $selectItems = $('.check-sel');
$selectAll.click(function () {
$selectItems.attr('checked', $selectAll.attr('checked'));
});
$selectItems.click(function () {
$selectAll.attr('checked', $selectItems.filter(':checked').length === $selectItems.length);
});
});
这篇关于javascript - 如何让全选选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文