views:

144

answers:

3

I have 20 Checkboxes. I need to disable 16 Checkboxes, if 4 checkboxes are selected.

I tryed this begann with this jquery code

    $("input[type=checkbox][name=cate]:checked").each(

    function()
    {
    }

);

What i need is if a user selects 4 checkboxes then all other checkboxes should be disabled.

+1  A: 

What you could do is the following (pseudo-jquery-javascript code, not tested ;) )

$("input[type=checkbox][name=cate]").click(function() {
    if($("input[type=checkbox][name=cate]:checked").length >= 4) {
        $("input[type=checkbox][name=cate]").not(":checked").attr("disable", "true");
    }
});

Something like that... ^^

x3ro
It doesnt work sorry
streetparade
That's why I said its pseudo-javascript... It's not supposed to work but to give you a clue on how you could implement it... Don't expect other ppl to write the code for you...
x3ro
+2  A: 
$("input[type=checkbox][name=cate]").click(function() {

    var bol = $("input[type=checkbox][name=cate]:checked").length >= 4;     
    $("input[type=checkbox][name=cate]").not(":checked").attr("disabled",bol);

});

demo

Reigel
I dont know but this also doesnt work
streetparade
see my demo... it's working...
Reigel
Yepp this works thanks have a nice evening
streetparade
A: 

Here is a working example. May be it would be usefull for some people ;-)

$.fn.limit = function(n) {
 var self = this;
 this.click(function(){
   (self.filter(":checked").length==n)?
     self.not(":checked").attr("disabled",true).addClass("alt"):
     self.not(":checked").attr("disabled",false).removeClass("alt");
 });
}

$("input:checkbox").limit(3);

}
streetparade