jquery 全選 全不選 事件綁定

來源:互聯網
上載者:User

標籤:cli   click   對象   實現   res   多個   efi   type   cti   

                  
<td width="82%" colspan="3">
<input type="checkbox" id="all">全選&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" id="reverse">反選
</td>
<td width="82%" colspan="3">
<s:checkboxlist name="resUuids" list="resList" listKey="uuid" listValue="name"></s:checkboxlist>
</td>

 

 

$(function(){
//全選
$("#all").click(function(){
//將下面所有組件全部選中
//$("[name=resUuids]")    是多個組件,整體是個對象數組
//$("[name=resUuids]").attr("checked","checked");
            
//先擷取當前組件的狀態
//$(this).attr("checked")
//將所有組件設定為對應狀態
//$("[name=resUuids]").attr("checked",$(this).attr("checked"));
            
//$(this).attr("checked")擷取的值究竟是什麼
//alert($(this).attr("checked"));        //undefined
//$("[name=resUuids]").attr("checked","undefined");
            
//js文法規則,除了false,FALSE,"false","FALSE",0五個值之外的所有值,認定為true
//$("[name=resUuids]").attr("checked",false);
            
var flag = $(this).attr("checked");
$("[name=resUuids]").attr("checked",flag == "checked");
});

 

//反選
        $("#reverse").click(function(){
            //將所有組件的狀態切換成原始狀態的反狀態
            
            //$("[name=resUuids]").attr("checked",!($("[name=resUuids]").attr("checked")=="checked"));
            
            //當選取器選中的組件是多個時,擷取組件的任何資料都是對第一個組件進行操作
            //alert(!($("[name=resUuids]").attr("checked")=="checked"));
            
            //對每個組件進行迭代,讓其操作狀態為對應組件的原始狀態的反狀態
            $("[name=resUuids]").each(function(){
                //使用each操作實現對每個組件的操作
                var flag = $(this).attr("checked");
                $(this).attr("checked", !(flag =="checked"));
            });
            checkSelect();
        });

 

//綁定組件
        $("[name=resUuids]").click(function(){
            //將全選的狀態設定為基於所有組件的綜合狀態值
            checkSelect();
        });
        
        function checkSelect(){
            var allFlag = true;
            $("[name=resUuids]").each(function(){
                var flag = $(this).attr("checked") == "checked";
                //&:位元運算與     &&:邏輯與
                allFlag = allFlag && flag;
            });
            $("#all").attr("checked",allFlag);
        }
        
    });

 

jquery 全選 全不選 事件綁定

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.