Can not limit the number of check boxes to be selected
<p>I'm using foreach loop to generate the checkboxes in my form. It is given below.</p>

<pre><code>&lt;?php $index = 0;
foreach($cameramen as $n_key){ ?&gt;
    &lt;div class="checkbox"&gt;
        &lt;label&gt;
            &lt;input type="checkbox" id="cameramen_&lt;?php echo $index+1; ?&gt;" name="cameramen" class="subscriber" value="&lt;?php echo $index+1; ?&gt;"&gt;

            &lt;?php 
                echo $n_key-&gt;emplist3; 
                $index++;
            ?&gt;

        &lt;/label&gt;
    &lt;/div&gt;
    &lt;br/&gt;&lt;br/&gt;
&lt;?php } ?&gt;  
</code></pre>

<p>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).</p>

<pre><code> $("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');
    }
  });
</code></pre>

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