Only Allow 2 Checkboxes Checked
I wanna say i rarely work with javascript. It's never been my thing and it probably never will be, so i'm sorry for my lack of knowledge to this language.. Which is why my question
Solution 1:
Are you looking for something like this?
$(':checkbox[name=weapon]').on('click',function(){
var checkedBoxlength=$(':checkbox[name=weapon]:checked').length;
if(checkedBoxlength>2){
alert('You can check a maximum of 2 boxes.');
returnfalse;
}
});
--- Edit
Just made a jquery plugin just for limiting how many chekboxes you check.
(function( $ ){
$.fn.LimitCheckableNumber = function( options ) {
var settings = $.extend( {
'nr' : 1,
}, options);
returnthis.each(function() {
var $this=$(this),
thisName=$this.attr('name');
$this.on('click',function(){
var count=$(":checked[name='"+thisName+"']").length;
if(count>settings.nr){
alert('You can check a maximum of '+settings.nr+' boxes. ');
returnfalse;
};
});
});
};
})( jQuery );
Solution 2:
You can't use the []
when using dot notation. Switch to treating it like an associative array.
checkboxlimit(document.forms.checkform['weapon[]'], 2)
Works just fine.
Also, you don't need to redefine checkgroup
and limit
. You can just use them as the arguments.
Post a Comment for "Only Allow 2 Checkboxes Checked"