jeudi 8 décembre 2016

Can not limit the number of check boxes to be selected

I'm using foreach loop to generate the checkboxes in my form. It is given below.

       <?php $index = 0;
       foreach($cameramen as $n_key){ ?>
            <div class="checkbox">
            <label><input type="checkbox" id="cameramen_<?php echo $index+1; ?>" name="cameramen" class="subscriber" value="<?php echo $index+1; ?>">
            <?php echo $n_key->emplist3; 
            $index++;?></label></div>
             <br/><br/>
            <?php 
            }?>  

I want to limit the number of checkboxes to be selected by the user to two. I used the following javascript function but it not worked(It allows to select all the checkboxes).

 $("input[name=cameramen]").change(function(){
    var max= 2;
    console.log($("input[name=cameramen]:checked").length);
    if( $("input[name=cameramen]:checked").length == max ){
        $("input[name=cameramen]").attr('disabled', 'true');
        $("input[name=cameramen]:checked").removeAttr('disabled');
    }else{
         $("input[name=cameramen]").removeAttr('disabled');
    }
  });

Someone please help me to solve this problem which I have being struggling for hours to solve.




Aucun commentaire:

Enregistrer un commentaire