1

How would i be able to disable the rest of the checkboxes once the selected items is 5? I was thinking there could be a way to do this in jquery. Here is my code below.

<table>
@{ var i = 0;}
@foreach (var testimonials in Model.Testimonials)
{
<tr>
    <td style="padding: 2px 0 2px 2px;">
        @Html.CheckBox("Testimonials[" + i.ToString() + "].DisplayTestimonials", testimonials.DisplayTestimonials.Value, new { @class = "chkItems" })
        @Html.Hidden("Testimonials[" + i.ToString() + "].ResponseId", testimonials.ResponseId.ToString())
    </td>
    <td style="padding: 2px 0 2px 2px;">@Html.TextBox("Testimonials[" + i.ToString() + "].FirstName", testimonials.FirstName, new { @readonly = "readonly", @class = "TextBoxAsLabel" })</td>
    <td style="padding: 2px 0 2px 2px;">@Html.TextBox("Testimonials[" + i.ToString() + "].LastName", testimonials.LastName, new { @readonly = "readonly", @class = "TextBoxAsLabel" })</td>
    <td style="padding: 2px 0 2px 2px;">@Html.TextBox("Testimonials[" + i.ToString() + "].Question5Answer", testimonials.Question5Answer.ToString(), new { @readonly = "readonly", @class = "TextBoxAsLabel" })</td>
</tr>
                                   i++;
}

Solution:

<script type="text/javascript">
$(document).ready(function () {
    function countchecked() {
        var n = $("input:checked").length;
        if (n >= 5)
            $('.chkItems:not(:checked)').attr("disabled", "disabled");
        else
            $('.chkItems:not(:checked)').removeAttr("disabled");
    }
    $(":checkbox").click(countchecked);
});

3 Answers 3

1

Here is my proposed solution:

 var checkedcount=0;
  $("input.chkItems").change(function(){
    if(this.checked) checkedcount++; else checkedcount--;
    if(checkedcount>=5)  $("input.chkItems:not(:checked)").attr("disabled", "disabled");
    else $("input.chkItems:not(:checked)").removeAttr("disabled");
  });

Working demo at: http://jsbin.com/etici4

EDITED: changed the event to "onchange" as suggested

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Ariel, This is what i have from your answer. But the checkboxes don't disable once the count goes over 5.<script type="text/javascript"> $(document).ready(function () { var checkedcount = 0; $("input.chkItems").click(function () { if (this.checked) checkedcount++; else checkedcount--; if (checkedcount >= 5) $("input.chkItems:not(:checked)").attr("disabled", "disabled"); else $("input.chkItems:not(:checked)").removeAttr("disabled"); }); }); </script>
To make it accessible, I'd recommend putting the event on change instead of on click. If someone is changing those boxes using a device other than the mouse, the limit may not kick in.
Maybe is a problem with the jquery selectors, in the sample I made I used a more generic selector. Try opening the browser console and make sure $("input.chkItems") returns the array of checkboxes in the page. Also check the :checked and :not(:checked) variants.
okay, i just tried doing $(document).ready(function () { var checkedcount = 0; $('.chkItems').change(function () {alert('Handler for change'); }); }); When i check the checkbox, the alert message comes up. It means the selector (.chkItems) work.
1

Below is an example (refined as suggested). You may want to refine the selectors.

<script type="text/javascript">
    jQuery(function() {
        jQuery("input:checkbox.chkItems").change(
            function() {
                var p = $(this).parent();
                var e = p.find("input:checked").length >= 5;
                p.find("input:not(:checked)").attr('disabled', e ? 'disabled' : '');
            }
        );
    });
</script>

<div id="checkbox-group">
    <input type="checkbox" class="chkItems" />
    <input type="checkbox" class="chkItems" />
    <input type="checkbox" class="chkItems" />
    <input type="checkbox" class="chkItems" />
    <input type="checkbox" class="chkItems" />
    <input type="checkbox" class="chkItems" />
    <input type="checkbox" class="chkItems" />
    <input type="checkbox" class="chkItems" />
    <input type="checkbox" class="chkItems" />
    <input type="checkbox" class="chkItems" />
    <input type="checkbox" class="chkItems" />
</div>

2 Comments

Looks good, but you'd really want to bind the change event via jQuery rather than the onchange attribute on each checkbox.
A good suggestion ... it also saves me from from defining the selector in multiple places, which is a bit evil. Incorporated! :)
0

just thinking maybe make an array to track the number of checked boxes the something like enabled=false if(array.length==5 && self!=checked)

that might work if you write a little javascript I am not familiar enough with jQuery to say whether there is some easy built in way to do it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.