vendredi 3 juillet 2015

Call jQuery function for each checkbox on Check/Uncheck All

I have a table with multiple checkbox. For each time I check or uncheck a checkbox, it calls a function that send the parameters as a Model to the controller to update the database. Now I want to check/uncheck All, doing the same thing. I can check/uncheck them all, but the function is not called.

The checkbox

<input id="Include" class="checkBoxClass" type="checkbox" @(item.Include != "1" ? null : "checked")>

The function that sends the model to the controller

$('input:checkbox').change(function () {
        var tr = $(this).parents('tr:first');
        var Id = document.getElementById('Id').value;
        (...)
        var UserModel =
        {
            "Id": Id,
            (...)

        };
        $.ajax({
            url: '@Url.Action("Action","Controller")',
            type: 'POST',
            data: JSON.stringify(UserModel),
            contentType: 'application/json; charset=utf-8',
            success: function () {
            }
        });
    });

And my check/uncheck all code

$('#ckbCheckAll').click(function (event) { 
            $('.checkBoxClass').each(function () {
                if (!this.checked)
                {
                $('.checkBoxClass').each(function () { 
                    $('.checkBoxClass').click("checked", true);
                });
                } else {
                $('.checkBoxClass').each(function () { 
                    $('.checkBoxClass').click("checked", false);
                });
            }
            });
        });

When I click the #ckbCheckAll button, all checkbox are checked/uncheked, but even that I have the .change on my "send model to controller" function, it dont work.

  • What I'm doing wrong here?
  • It's reliable doing this way?



Aucun commentaire:

Enregistrer un commentaire