In a checkbox list, know which ones are checked

Asked

Viewed 1,157 times

5

In my application I am printing a list of data coming database, and in each item of the list I am placing a checkbox, as shown in the following picture:

inserir a descrição da imagem aqui

Now by clicking "Start copy" I want to select the items from the list that are selected, and the respective 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>

To ViewBag.estabelecimentos is my list of data returned from controller.

Now to fetch the values of the selected checkbox I am doing (javascript):

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

In the code above I’m just trying to get the value from each selected checkbox, then send to the controller and start the data copy. But without success... I can’t get the selected Dropbox

1 answer

7


You can do it this way:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.