2011-06-14 126 views

回答

6

警告,無恥的自我推銷提前。

我寫了一個jQuery的插件,完成這種事情。看一看:http://mjball.github.com/jQuery-CheckAll

要與你鏈接的頁面上的標記使用它:

<form action="#" method="post" id="myform"> 

<fieldset> 
    <div class="radio"><input type="checkbox" name="checkall" id="checkall"> <label for="checkall">Check all</label></div> 
    <div class="radio"><input type="checkbox" name="checkbox1" id="checkbox1"> <label for="checkbox1">Checkbox</label></div> 
    <div class="radio"><input type="checkbox" name="checkbox2" id="checkbox2"> <label for="checkbox2">Checkbox</label></div> 
    <div class="radio"><input type="checkbox" name="checkbox3" id="checkbox3"> <label for="checkbox3">Checkbox</label></div> 
    <div class="radio"><input type="checkbox" name="checkbox4" id="checkbox4"> <label for="checkbox4">Checkbox</label></div> 
    <div class="radio"><input type="checkbox" name="checkbox5" id="checkbox5"> <label for="checkbox5">Checkbox</label></div> 
    <div class="radio"><input type="checkbox" name="checkbox6" id="checkbox6"> <label for="checkbox6">Checkbox</label></div> 
    <div class="radio"><input type="checkbox" name="checkbox7" id="checkbox7"> <label for="checkbox7">Checkbox</label></div> 
    <div class="radio"><input type="checkbox" name="checkbox8" id="checkbox8"> <label for="checkbox8">Checkbox</label></div> 
    <div class="radio"><input type="checkbox" name="checkbox9" id="checkbox9"> <label for="checkbox9">Checkbox</label></div> 
    <div class="radio"><input type="checkbox" name="checkbox10" id="checkbox10"> <label for="checkbox10">Checkbox</label></div> 
</fieldset> 

</form> 

這個就足夠了:

$('#checkall').checkAll('#myform input:checkbox:not(#checkall)'); 

演示:http://jsfiddle.net/mattball/npeTz/

CheckAll正常工作與jQuery 1.4.4和1.5.2。我沒有時間爲jQuery 1.6進行更新。 對不起。


更新的jQuery 1.6兼容性:http://jsfiddle.net/mattball/CVQsp/


它仍然有效,即使你不小心選擇主與奴一起:http://jsfiddle.net/mattball/BwFvc/

+1

即使是無恥的自我推銷,如果你不能在20分鐘左右自己寫代碼,我總是會推薦使用插件.... – jcolebrand 2011-06-14 04:20:42

+0

Matt Ball,如果jquery是1.26 ,代碼無法工作。 – zhuanzhou 2011-06-14 05:33:42

+0

@zhuanzhou你能不能更新到至少1.4.2? [jQuery 1.2.6](http://code.jquery.com/jquery-1.2.6.js)是**真**老(它發佈了2008-05-24 - 3年多前)。 – 2011-06-14 15:04:21

1

您需要處理的狀態變化的孩子複選框。 試試這個:

<script type="text/javascript"> 
$(function() { 
    $('#checkall').click(function() { 
     $(this).parents('fieldset:eq(0)').find(':checkbox').attr('checked', this.checked); 
    }); 
     //THIS IS NEW ADDITION TO YOUR CODE 
    $('input[id^="checkbox"]').click(function(){ 
    if(!this.checked){ 
     $('#checkall').attr("checked", false) 
    } 
    }); 
}); 
</script> 
+0

這是什麼意思input [id^=「checkbox thank you。 – zhuanzhou 2011-06-14 05:23:45

4

這是我的承擔。

var checkall = $('#checkall'); 
var boxes = $('input[type="checkbox"]').not(checkall); 

checkall.click(function() { 
    boxes.attr('checked', this.checked); 
}); 
boxes.change(function() { 
    checkall[0].checked = this.checked && boxes.filter(':checked').length === boxes.length; 
}); 

這樣checkall得到檢查,即使您手動檢查所有子框。

http://jsfiddle.net/T4EK2/

+0

'回合時間你出現':))' – 2011-06-14 05:11:31

+1

Howdy @Matt Ball。我怎麼能遠離?得到這個上癮!o) – user113716 2011-06-14 05:18:28

+0

這是什麼意思? ?checkall [0] .checked = this.checked && boxes.filter(':checked')。length === boxes.length;我可以改變這個boxes.attr('checked',this.checked); .attr('checked',checked);謝謝 – zhuanzhou 2011-06-15 03:59:19

1

好,也有類似的答案了。因爲我花了一些時間讓它工作和學習新東西,所以我會發布這個。 不適用於v1.6

http:// jsfiddle。淨/ gsndd/

http://jsfiddle.net/gsndd/2/

$(function() { 
    // Ripped from https://github.com/mjball/jQuery-CheckAll @Matt Ball 
    var propFn = typeof $.fn.prop === 'function' ? 'prop' : 'attr'; 

    $('#checkall').click(function() { 
     $(this).parents('fieldset:eq(0)').find(':checkbox')[propFn]('checked', this.checked); 
    }); 
    $("input[type=checkbox]:not(#checkall)").click(function() { 
     if (!this.checked) { 
      $("#checkall")[propFn]('checked', this.checked); 
     } else { 
      $("#checkall")[propFn]('checked', !$("input[type=checkbox]:not(#checkall)").filter(':not(:checked)').length); 
     } 

    }); 
}); 

編輯:做出同樣的工作,V1.6 +,只是改變 attrprop。最終,你最好使用一種適用於兩者的東西。

編輯 - 修復了在任何版本上工作的代碼(假設John不會推出v1.7並且說'驚喜')。 感謝@Matt Ball

+0

使它與兩個工作很容易 - 看看我的插件的來源。 – 2011-06-14 05:27:34

+0

@馬球 - 謝謝先生,我修好了。自從我通過回答SO上的問題來學習jQuery之後,它已經有10天了,而且這些提示都非常棒。 – Lobo 2011-06-14 05:45:45

+0

這是什麼意思? var propFn = typeof $ .fn.prop ==='function'? 'prop':'attr';謝謝 – zhuanzhou 2011-06-14 05:59:17