1.添加點擊事件、each、prop、$(this)


1 //全選框的被動操作 2 //定義一個標志保存最終狀態 3 var flag = false; 4 //為每一個選擇框添加點擊事件,數組.click() 5 $('.chex').click(function(){ 6 //遍歷數組,數組.each() 7 $('.chex').each(function(){ 8 //判斷每一個元素的點擊狀態,$(this)當前元素 9 if($(this).prop('checked')){ 10 flag = true; 11 }else{ 12 flag = false; 13 //不可以使用break 14 return false; 15 } 16 }); 17 if(flag){ 18 $('#chexAll').prop('checked',true); 19 }else{ 20 $('#chexAll').prop('checked',false); 21 } 22 });
?