In a checkbox list, know which ones are checked

5

In my application I'm printing a list of data coming from the database, and in each item in the list I'm putting a checkbox, as the following image shows:

NowwhenIclick"Start Copy" I want to select the items in the list that are selected, and their value .

Printing the list in the view I'm doing:

<div style="width: 40%; height: 60%; overflow-y: scroll;">
    @foreach (var item in ViewBag.estabelecimentos)
    {
        <input type="checkbox" value="@item.IdFilial" name="filialCopia"/>@item.Nome<br />
    }
</div>

<button type="button" class="btn btn-primary" onclick="FazerCopiaEstabs()">Iníciar cópia</button>

The ViewBag.estabelecimentos is my list of data returned from the controller.

Now to get the values of the selected checkboxes I'm doing (javascript):

function FazerCopiaEstabs() {
    var fields = $("input[name='filialCopia']").serializeArray();
    $.each(fields, function (index, itemData) {
        alert(itemData.val());
    })
}

In the above code I'm just trying to get the value of each checked checkbox, then send it to the controller and start copying data. But to no avail ... I'm not able to fetch the selected dropboxes

    
asked by anonymous 15.04.2014 / 13:18

1 answer

7

You can do this as follows:

function FazerCopiaEstabs() {
        $("input:checkbox[name=filialCopia]:checked").each(function () {
            alert(this.value);
        });
    }
    
15.04.2014 / 13:52