<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script src="jquery.js" type="text/javascript"></script>
<script>function selectAll(obj)
{
if(obj.checked==true)
{
$(":checkbox").filter("[name=my]").get().checked = true;
}
else
{  
$(":checkbox").filter("[name=my]").get().checked = false;
}
}</script>
</head>
<body>
<input name="my" type="checkbox"  value="1">图选1</input>
<input name="my" type="checkbox"  value="2">图选2</input>
<input name="my" type="checkbox"  value="3">图选3</input>
<input name="my" type="checkbox"  value="4">图选4</input>
<br/><br/>
<input type="checkbox"  onclick="selectAll(this)">全选/取消</input>
</body>
</html>

解决方案 »

  1.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>无标题文档</title>    <script type="text/javascript" src="Script/jquery-1.3.2.js"></script></head>
    <body>
        <input name="my" type="checkbox" value="1">图选1</input>
        <input name="my" type="checkbox" value="2">图选2</input>
        <input name="my" type="checkbox" value="3">图选3</input>
        <input name="my" type="checkbox" value="4">图选4</input>
        <br />
        <br />
        <input id="inputChkAll" type="checkbox">全选/取消</input>    <script type="text/javascript">        $('#inputChkAll').click(function() {
                $("input[name='my']").attr("checked", $(this).attr("checked"));
            });    </script>
    </body>
    </html>
      

  2.   

    这理请参考jQuery参考文档中get()$(":checkbox").filter("[name=my]").get()这是取得所有匹配元素的一种向后兼容的方式(不同于jQuery对象,而实际上是元素数组)。$(":checkbox").filter("[name=my]").get() 这个是一数组。给一数设置checked 当然不行了。这里是我修改后的代码,可作参考            function selectAll(obj) {
    alert( $(":checkbox").filter("[name=my]").get().length)
                    if (obj.checked == true) {
                        $(":checkbox").filter("[name=my]").get().checked = true;
    // $(":checkbox[name='my']").attr("checked",true);
                    } else {
                        $(":checkbox").filter("[name=my]").get().checked = false;
    // $(":checkbox[name='my']").removeAttr("checked");
                    }
                }
      

  3.   

    [code=JScrip]<script>function selectAll(obj)
    {
    if(obj.checked==true)
    {
    $("input:checkbox").filter("[name=my]").each(function(){this.checked=true});

    else
    {   
    $("input:checkbox").filter("[name=my]").each(function(){this.checked=false});
    }
    }</script>[/code]