disable checkboxes once the count is 5

Discussion in 'C#' started by DmitryS, Feb 25, 2011.

  1. #1
    Hi,

    I have a foreach loop that displays survey testimonials. The user is allowed to pick only 5 testimonials. Is there an easy way to disable the rest of the checkboxes once the selected item is 5? I was trying this in jquery but this is not doing anything. Any help or idea would be greatly appreciated. Thanks!!

    <script type="text/javascript">
    $(document).ready(function () {
    var checkedcount = 0;
    $('.chkItems').click(function () {
    if (this.checked)
    checkedcount++;
    else
    checkedcount--;
    if (checkedcount >= 5)
    $('.chkItems:not:)checked)').attr("disabled", "disabled");
    else
    $('.chkItems:not:)checked)').removeAttr("disabled");
    });
    });
    </script>

    @using (Html.BeginForm())
    {
    <table width="100%" cellpadding="0" cellspacing="0"
    rules="all">
    <thead>
    <tr>
    <td align="center" style="padding: 2px 0 2px 2px;">Select</td>
    <td align="center" style="padding: 2px 0 2px 2px;">First Name</td>
    <td align="center" style="padding: 2px 0 2px 2px;">Last Name</td>
    <td align="center" style="padding: 2px 0 2px 2px;">Testimonial</td>
    </tr>
    </thead>
    @{ 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++;
    }
    </table>
    }
     
    DmitryS, Feb 25, 2011 IP