1

I am working on a MVC app and right now I am stuck on the following problem: I have checkboxes generated from one of my models, however I don't know how to get the value(either name of id) of the selected ones in javascript. Any ideas ?

Here's the code:

@if (Model.Controls.Any())
       {

           for (int x = 0; x < Model.Controls.Count(); x++)
           {
        <div aria-autocomplete="inline">

            @Html.CheckBoxFor(p => p.Controls[x].IsSelected, new { @class = "CCB" })
            @Html.Label(Model.Controls[x].Name)
            @Html.HiddenFor(p => p.Controls[x].ID)

        </div>
           }

       }

Working with :

$(document).ready(function () {
    $('.CCB').change(function (event) {
        var matches = [];
        $(".CCB:checked").each(function () {
            matches.push(this.value);
        });
        alert(matches);

    });
});

1 Answer 1

2

Try:

$(document).ready(function () {
    $('.CCB').change(function (event) {
        if ($(this).is(':checked'))
        {
            var theId = $(this).attr('id'); // The id of the checkbox
            var theValue = $(this).val(); // The value field of the checkbox
        }
    });
});

See also How can I get checkboxlist currently selected item value.

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

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.