Dynamically limit the amount of checkboxes that can be checked by user
<p>I've created a test and answer system. Each question can either have one or multiple answers, e.g "Please mark 1 answer", "Please mark 3 answers" etc.</p>

<p>Is it possible to limit the amount of checkboxes that the user can check based on this? If the question requires 1 answer, then they can only check 1 of the checkboxes. </p>

<pre><code>&lt;?php foreach ($rows as $row){ ?&gt;
   &lt;input type="checkbox" name="answer&lt;?php echo $counter; ?&gt;[]" value="&lt;?php echo the_sub_field('answer'); ?&gt;" /&gt;
   &lt;?php echo $row['answer']; ?&gt;
&lt;?php } ?&gt;
</code></pre>

<p>It loops through this and pulls in each answer, the amount of answers needed for each questions is dynamic and is controlled by this field (where it can be 1, 2, 3 etc):</p>

<pre><code>&lt;?php echo $repeater_row['required_answers']; ?&gt;
</code></pre>

<hr>

<p>Using the jquery below, although this isn't dynamic:</p>

<pre><code>$(document).ready(function () {
    if ($("input:checkbox:checked").length &gt;= 3) {
        $("input:checkbox").not(":checked").attr("disabled", 3);
    }
    $("input:checkbox").click(function () {
        var bol = $("input:checkbox:checked").length &gt;= 3;
        $("input:checkbox").not(":checked").attr("disabled", bol);
    });
});
</code></pre>

<p><strong>How can I do it dynamically? Each question has it's answers named in sets, so the first question has an answer set called with their inputs named <code>name="answer1[]"</code> the second question has an answer set <code>name="answer2[]"</code> and so on.</strong></p>
