简体   繁体   中英

Html5 support having SelectAll for checkboxes

i have the following Razor view inside my asp.net mvc web application:-

foreach (var item in Model) {
    <tr id="@item.RouterID">
        <td>

 <input type="checkbox" name="CheckBoxSelection" 
                               value="@item.RouterID.ToString()"
                                /> 
@Html.ActionLink("Edit", "Edit","Router", new { id=item.RouterID },null)

Which will display a checkboxes inside table rows. so does Html-5 support having SelectAll checkbox , for selecting all the checkboxes that have the same name or id ? Thanks

Edit i add the following selectAll checkbox:-

<input type="checkbox" 
                               name="selectAll"
                id="selectAll" 

                                /> 

and i wrote the following script:-

$('body').on("change", "#selectAll", function () {
    var checkflag = "false";
    var checkboxes = document.getElementsByName('CheckBoxSelection');
    function check() {
        if (checkflag == "false") {
            for (i = 0; i < checkboxes.length; i++) {
                checkboxes[i].checked = true;
            }
            checkflag = "true";

        } else {
            for (i = 0; i < checkboxes.length; i++) {
                field[i].checked = false;
            }
            checkflag = "false";

        }
    }
});

but it did not work,, i mean the checkboxes will never get checked/unchekced .. can anyone adivce?

Add a checkbox for Select All / Deselect All

<input type="checkbox" id="selectAll" name="selectAll" />

Add a css class named "checkBoxClass"

foreach (var item in Model) {
    <tr id="@item.RouterID">
        <td>

 <input class='checkBoxClass' type="checkbox" name="CheckBoxSelection" 
                               value="@item.RouterID.ToString()"
                                /> 
@Html.ActionLink("Edit", "Edit","Router", new { id=item.RouterID },null)
}

In jquery

 $(document).ready(function () {
    $("#selectAll").click(function () {
        $(".checkBoxClass").prop('checked', $(this).prop('checked'));
    });
});

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