2012-09-05 145 views
1

在鉻,沒有錯誤,沒有警告,甚至沒有信息。 它只是不工作。單擊事件不起作用

$(document).ready(function() { 
     var cbxAdmins = $("input[name^=hasAdmin][type=checkbox]"); 
     for (var i = 0; i < cbxAdmins.Length; i++) { 

      cbxAdmins[i].click(function() { 
       var checkAll = this.checked; 
       var permiCheckboxes = $(this).parents("tr:first").find(':checkbox'); 

       if (checkAll) { 
        $(permiCheckboxes).attr('checked', true); 
       } 
       else { 
        $(permiCheckboxes).attr('checked', ''); 
       } 
      }); 
     } 


    }); 
+0

你確定這是點擊事件嗎?如果您在cilck委託的第一行中放置了警報/跟蹤,那麼它是否也不會觸發? –

+1

你檢查過你的css選擇器嗎? 'cbxAdmins.Length'的價值是什麼? – Jan

+0

你檢查了cbxAdmins [i]的值嗎? –

回答

4

你不需要跨陣列中的所有元素的for循環,jQuery是足夠聰明,單獨應用點擊事件。試試這個:

$(document).ready(function() { 
    $("input[name^=hasAdmin][type=checkbox]").click(function() { 
     $(this).closest("tr").find('[type=checkbox]').prop('checked', this.checked); 
    }); 
}); 

我也清理了一點邏輯,謝謝Fabricio Matte。

+0

也可以使用'.prop'而不是'.attr' –

+0

@FabrícioMatté老習慣難度很大:) –

+0

+1,我開始在版本1.7上使用jQuery,所以我沒有1.6的經驗使用'.attr'爲屬性。 '=]' –