Jquery 實現全選反選功能

<!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>
    <title></title>
     <style type="text/css">
    table img
    {
        width:50px; height:50px;
        }
        table
        {
           width:600px; height:400px;
           }
           table tr td{ border:solid 1px #eee;}
         #img1
         {
             position:absolute;width:200px;height:200px;display:none;
             } 
    </style>

    <script src="Jquery1.7.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $('#chkall').click(function () {
                /*if ($(this).is(':checked') == true) {
                $('table input[type=checkbox]').attr('checked', true);
                }
                else {
                $('table input[type=checkbox]').attr('checked', false);
                }*/
                //簡單寫法
                $('table input[type=checkbox]').attr('checked', $(this).is(':checked'));
                if (this.checked) {
                    $('#sp1').text('取消全選');
                }
                else {
                    $('#sp1').text('全選');
                }
                //alert($('#chkall').attr('checked'));
                //另一種方法,使用js獲取全選複選框的選中狀態
                //$('table input[type=checkbox]').attr('checked',this.checked);
            })
            $('#chkrevert').click(function () {
                //方法1:
                /*var allcheck = $('table input[type=checkbox]');
                for (var i = 0; i < allcheck.length; i++) {
                if (allcheck[i].checked) {
                allcheck[i].checked = false;
                }
                else {
                allcheck[i].checked = true;
                }
                }*/

                var allcheck = $('table input[type=checkbox]');
                allcheck.each(function () {
                    if ($(this).is(':checked')) {
                        $(this).attr('checked', false);
                    }
                    else {
                        $(this).attr('checked', true);
                    }
                })
            })

            $('table img').mousemove(function (e) {
                $('.imgclass').attr('src', $(this).attr('src'));
                $('.imgclass').css('top', e.pageY + 0);
                $('.imgclass').css('left', e.pageX + 50);
                $('.imgclass').show();
            })
            $('table img').mouseleave(function () {
                $('.imgclass').hide();
            })
        })
    </script>

</head>
<body>

<img src="" id="img1" class="imgclass"/>
 <div>
    <input id="chkall" type="checkbox" /><span id="sp1">全選</span><input id="chkrevert" type="checkbox" />反選</div>
    <table>
        <tr>
            <td>
                操作
            </td>
            <td>
                編號
            </td>
            <td>
                名稱
            </td>
            <td>
                作者
            </td>
            <td>
                封面
            </td>
            <td>
                單價
            </td>
        </tr>
        <tr>
            <td>
                <input id="Checkbox1" type="checkbox" />
            </td>
            <td>
                001
            </td>
            <td>
                三國演義
            </td>
            <td>
                羅貫中
            </td>
            <td>
                <img src="images/2.jpg" />
            </td>
            <td>
                45
            </td>
        </tr>
        <tr>
            <td>
                <input id="Checkbox2" type="checkbox" />
            </td>
            <td>
                002
            </td>
            <td>
                西遊記
            </td>
            <td>
                吳承恩
            </td>
            <td>
                <img src="images/3.jpg" />
            </td>
            <td>
                40
            </td>
        </tr>
        <tr>
            <td>
                <input id="Checkbox3" type="checkbox" />
            </td>
            <td>
                003
            </td>
            <td>
                水滸傳
            </td>
            <td>
                施耐庵
            </td>
            <td>
                <img src="images/4.jpg" />
            </td>
            <td>
                50
            </td>
        </tr>
        <tr>
            <td>
                <input id="Checkbox4" type="checkbox" />
            </td>
            <td>
                004
            </td>
            <td>
                紅樓夢
            </td>
            <td>
                曹雪芹
            </td>
            <td>
                <img src="images/5.jpg" />
            </td>
            <td>
                100
            </td>
        </tr>
        <tr>
            <td>
                <input id="Checkbox5" type="checkbox" />
            </td>
            <td>
                005
            </td>
            <td>
                家
            </td>
            <td>
                巴金
            </td>
            <td>
                <img src="images/6.jpg" />
            </td>
            <td>
                45
            </td>
        </tr>
        <tr>
            <td>
                <input id="Checkbox6" type="checkbox" />
            </td>
            <td>
                006
            </td>
            <td>
                茶館
            </td>
            <td>
                老舍
            </td>
            <td>
                <img src="images/7.jpg" />
            </td>
            <td>
                35
            </td>
        </tr>
    </table>

</body>
</html>

 

發佈了133 篇原創文章 · 獲贊 3 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章