简体   繁体   中英

How can I select all checkboxes in mvc

This code below select only one by one checkbox, how can I transform this code so I can select all checkboxes by one click, but I need my variables to stay defined.

$('.checktip').click(function () {
    var iduser = $('.iduser').val();
    var idtip = $(this).attr('idtip');
    var che = $(this).prop('checked');
    $.ajax({
        url: UrlSettingsDocument.OrgUnits,
        data: { iduser: iduser, idtip: idtip, che: che },
        type: "POST",
        success: function (result) {
            if (result.result == "Redirect") {
                window.location = result.url;
            }
        }
    });
});

I using this variables for controller where I save this values in database when I check them or unchecked.

Here is my html code

<input type="hidden" value="@ViewBag.iduser" id="iduser" />
<hr />
<table class="table table-striped grid-table">
    <tr>
        <th>Samp</th>
        <th>Id of Book</th>
        <th>
        //Checkbox for check all *(NOT IMPLEMENTED)*
        <input type="checkbox"  id="box" name="checkAll" />
        </th>
    </tr>
    @foreach (var item in (IEnumerable<cit.Models.getCheIdTip_Result>)Model)
    {
    <tr>
        <td>@item.idtip</td>
        <td>@item.tipname</td>
        <td>

            @*@Html.CheckBox(item.id.ToString(), item.iduser == ViewBag.iduser ? true : false, new { idtip = item.idtip, @class = "checktip" })*@

            <div class="pure-checkbox">
                <input type="checkbox" idtip="@item.idtip" class="checktip" checked="@(item.iduser == ViewBag.iduser ? true : false)" name="@item.id.ToString()" id="@item.id.ToString()" />
                <label for="@item.id.ToString()"></label>
            </div>
        </td>
    </tr>
    }
</table>

Following code should work with your code

$('#box').click(function(){ $('.checktip').prop('checked', true); }};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM