jQuery操作checkbox選擇

1、checkbox list選擇

效果圖:

 

\

代碼:

[html]
<!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 runat="server"> 
    <title></title> 
    <script src="Scripts/jquery-1.7.min.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
        $(function () { 
            // 全選 
            $("#btnCheckAll").bind("click", function () { 
                $("[name = chkItem]:checkbox").attr("checked", true); 
            }); 
  
            // 全不選 
            $("#btnCheckNone").bind("click", function () { 
                $("[name = chkItem]:checkbox").attr("checked", false); 
            }); 
  
            // 反選 
            $("#btnCheckReverse").bind("click", function () { 
                $("[name = chkItem]:checkbox").each(function () { 
                    $(this).attr("checked", !$(this).attr("checked")); 
                }); 
            }); 
  
            // 全不選 
            $("#btnSubmit").bind("click", function () { 
                var result = new Array(); 
                $("[name = chkItem]:checkbox").each(function () { 
                    if ($(this).is(":checked")) { 
                        result.push($(this).attr("value")); 
                    } 
                }); 
  
                alert(result.join(",")); 
            }); 
        }); 
    </script> 
</head> 
<body> 
    <p> 
        <input name="chkItem" type="checkbox" value="今日話題" />今日話題 
        <input name="chkItem" type="checkbox" value="視覺焦點" />視覺焦點 
        <input name="chkItem" type="checkbox" value="財經" />財經 
        <input name="chkItem" type="checkbox" value="汽車" />汽車 
        <input name="chkItem" type="checkbox" value="科技" />科技 
        <input name="chkItem" type="checkbox" value="房產" />房產 
        <input name="chkItem" type="checkbox" value="旅遊" />旅遊 
    </p> 
    <p> 
        <input id="btnCheckAll" type="button" value="全選" /> 
        <input id="btnCheckNone" type="button" value="全不選" /> 
        <input id="btnCheckReverse" type="button" value="反選" /> 
        <input id="btnSubmit" type="button" value="提交" /> 
    </p> 
</body> 
</html> 

<!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 runat="server">
    <title></title>
    <script src="Scripts/jquery-1.7.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            // 全選
            $("#btnCheckAll").bind("click", function () {
                $("[name = chkItem]:checkbox").attr("checked", true);
            });
 
            // 全不選
            $("#btnCheckNone").bind("click", function () {
                $("[name = chkItem]:checkbox").attr("checked", false);
            });
 
            // 反選
            $("#btnCheckReverse").bind("click", function () {
                $("[name = chkItem]:checkbox").each(function () {
                    $(this).attr("checked", !$(this).attr("checked"));
                });
            });
 
            // 全不選
            $("#btnSubmit").bind("click", function () {
                var result = new Array();
                $("[name = chkItem]:checkbox").each(function () {
                    if ($(this).is(":checked")) {
                        result.push($(this).attr("value"));
                    }
                });
 
                alert(result.join(","));
            });
        });
    </script>
</head>
<body>
    <p>
        <input name="chkItem" type="checkbox" value="今日話題" />今日話題
        <input name="chkItem" type="checkbox" value="視覺焦點" />視覺焦點
        <input name="chkItem" type="checkbox" value="財經" />財經
        <input name="chkItem" type="checkbox" value="汽車" />汽車
        <input name="chkItem" type="checkbox" value="科技" />科技
        <input name="chkItem" type="checkbox" value="房產" />房產
        <input name="chkItem" type="checkbox" value="旅遊" />旅遊
    </p>
    <p>
        <input id="btnCheckAll" type="button" value="全選" />
        <input id="btnCheckNone" type="button" value="全不選" />
        <input id="btnCheckReverse" type="button" value="反選" />
        <input id="btnSubmit" type="button" value="提交" />
    </p>
</body>
</html>
2、checkbox table選中

效果圖:

 \
代碼:

[html]
!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 runat="server"> 
    <title></title> 
    <style type="text/css"> 
        table 
        { 
            border-collapse: collapse; 
        } 
        td 
        { 
            border: 1px solid #ccc; 
        } 
    </style> 
    <script src="Scripts/jquery-1.7.min.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
        $(function () { 
            // chkAll全選事件 
            $("#chkAll").bind("click", function () { 
                $("[name = chkItem]:checkbox").attr("checked", this.checked); 
            }); 
  
            // chkItem事件 
            $("[name = chkItem]:checkbox").bind("click", function () { 
                var $chk = $("[name = chkItem]:checkbox"); 
                $("#chkAll").attr("checked", $chk.length == $chk.filter(":checked").length); 
            }) 
        }); 
    </script> 
</head> 
<body> 
    <table id="tb"> 
        <thead> 
            <tr> 
                <td> 
                    <input id="chkAll" type="checkbox" /> 
                </td> 
                <td> 
                    分類名稱 
                </td> 
            </tr> 
        </thead> 
        <tbody> 
            <tr> 
                <td> 
                    <input name="chkItem" type="checkbox" value="今日話題" /> 
                </td> 
                <td> 
                    今日話題 
                </td> 
            </tr> 
            <tr> 
                <td> 
                    <input name="chkItem" type="checkbox" value="視覺焦點" /> 
                </td> 
                <td> 
                    視覺焦點 
                </td> 
            </tr> 
            <tr> 
                <td> 
                    <input name="chkItem" type="checkbox" value="財經" /> 
                </td> 
                <td> 
                    財經 
                </td> 
            </tr> 
            <tr> 
                <td> 
                    <input name="chkItem" type="checkbox" value="汽車" /> 
                </td> 
                <td> 
                    汽車 
                </td> 
            </tr> 
            <tr> 
                <td> 
                    <input name="chkItem" type="checkbox" value="科技" /> 
                </td> 
                <td> 
                    科技 
                </td> 
            </tr> 
            <tr> 
                <td> 
                    <input name="chkItem" type="checkbox" value="房產" /> 
                </td> 
                <td> 
                    房產 
                </td> 
            </tr> 
            <tr> 
                <td> 
                    <input name="chkItem" type="checkbox" value="旅遊" /> 
                </td> 
                <td> 
                    旅遊 
                </td> 
            </tr> 
        </tbody> 
    </table> 
</body> 
</html> 

<!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 runat="server">
    <title></title>
    <style type="text/css">
        table
        {
            border-collapse: collapse;
        }
        td
        {
            border: 1px solid #ccc;
        }
    </style>
    <script src="Scripts/jquery-1.7.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            // chkAll全選事件
            $("#chkAll").bind("click", function () {
                $("[name = chkItem]:checkbox").attr("checked", this.checked);
            });
 
            // chkItem事件
            $("[name = chkItem]:checkbox").bind("click", function () {
                var $chk = $("[name = chkItem]:checkbox");
                $("#chkAll").attr("checked", $chk.length == $chk.filter(":checked").length);
            })
        });
    </script>
</head>
<body>
    <table id="tb">
        <thead>
            <tr>
                <td>
                    <input id="chkAll" type="checkbox" />
                </td>
                <td>
                    分類名稱
                </td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>
                    <input name="chkItem" type="checkbox" value="今日話題" />
                </td>
                <td>
                    今日話題
                </td>
            </tr>
            <tr>
                <td>
                    <input name="chkItem" type="checkbox" value="視覺焦點" />
                </td>
                <td>
                    視覺焦點
                </td>
            </tr>
            <tr>
                <td>
                    <input name="chkItem" type="checkbox" value="財經" />
                </td>
                <td>
                    財經
                </td>
            </tr>
            <tr>
                <td>
                    <input name="chkItem" type="checkbox" value="汽車" />
                </td>
                <td>
                    汽車
                </td>
            </tr>
            <tr>
                <td>
                    <input name="chkItem" type="checkbox" value="科技" />
                </td>
                <td>
                    科技
                </td>
            </tr>
            <tr>
                <td>
                    <input name="chkItem" type="checkbox" value="房產" />
                </td>
                <td>
                    房產
                </td>
            </tr>
            <tr>
                <td>
                    <input name="chkItem" type="checkbox" value="旅遊" />
                </td>
                <td>
                    旅遊
                </td>
            </tr>
        </tbody>
    </table>
</body>
</html>

 

發佈留言