Reputation: 1625
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);
});
});
Upvotes: 1
Views: 5831
Reputation: 1839
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.
Upvotes: 2