注册 登录
编程论坛 JavaScript论坛

jquery checkbox问题

wxm198427 发布于 2011-03-30 15:57, 581 次点击
在JQUERY中,我这样写:$(function(){$("#selectall").toggle(function(){$(".chk").attr("checked",true);},function(){$(".chk").attr("checked",false);})});是当我在全选这个checkbox打勾后,类为chk的checkbox全选中,但这个时候全选这个checkbox并没有打勾,但是用click呢,又不能触发第二函数,只能全选却全否不了,不知道为什么?

[ 本帖最后由 wxm198427 于 2011-3-30 18:00 编辑 ]
3 回复
#2
aspic2011-03-31 14:44
toggle方法好像是切换显示/隐藏的吧
#3
aspic2011-03-31 14:58
程序代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. xmlns="http://www. http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src="Js/jquery.js"></script>
<script type="text/javascript">
$(function(){
    $("#selectall").click(function () {
        $(".chk").attr('checked', this.checked)
    })
})
</script>
</head>

<body>
<label><input type="checkbox" id="selectall" />全选</label>
<p>
    <label><input type="checkbox" class="chk" />1</label>
    <label><input type="checkbox" class="chk" />2</label>
    <label><input type="checkbox" class="chk" />3</label>
    <label><input type="checkbox" class="chk" />4</label>
    <label><input type="checkbox" class="chk" />5</label>
    <label><input type="checkbox" class="chk" />6</label>
    <label><input type="checkbox" class="chk" />7</label>
    <label><input type="checkbox" class="chk" />8</label>
    <label><input type="checkbox" class="chk" />9</label>
    <label><input type="checkbox" class="chk" />10</label>
    <label><input type="checkbox" class="chk" />11</label>
    <label><input type="checkbox" class="chk" />12</label>
</p>
</body>
</html>
#4
等自行车的鱼2011-05-31 11:38
$(function(){
    $("#selectall").click(function () {
        $(".chk").attr('checked', this.checked)
    })
})
真不错
1