0

I have a grid that has grouping i.e the grid shows products of group. So, the structure has Group and its childs. Same way I will have some other groups and their childs which I am binding like below.I want to check and uncheck the entire sublist on the click of parent.

CSHTML
@{
    if (Model.List.Count() > 0)
    {
    <table>
        <tr>
              <input type="checkbox" class="cls@(obj.GroupId)" name="@obj.GroupId" onclick="checkUncheckAll(this.class)" />
            <td>Name</td>
          </tr>
        @foreach (var obj in Model.subList)
        {
            <tr>
                <td>
                    <input type="checkbox" class="cls@(obj.GroupId)" name="@obj.GroupId" /></td>
                </td>
                <td>
                    @obj.Name
                </td>
            </tr>
    </table>
    }
}


<input type="button" value="Save" onclick="PickAllCheckedRows();" />

I have been trying to do this like below but no success. Also I want to pick all the checked group and its subitems on click of save button. Any help would be really appreciated.

<script type="text/javascript">
    function checkUncheckAll(sender) {
        alert(sender);
        var chkElements = document.getElementsByClassName('chkItems');
        for (var i = 0; i < chkElements.length; i++) {
            chkElements[i].checked = sender.checked;
        }
    }

  function PickAllCheckedRows() {
}
</script>
4

1 回答 1

1

您正在调用您的函数传入,undefined因为您正在传递this.class

onclick="checkUncheckAll(this.class)"

元素没有class属性(它们确实有className),如果有,你不想传递它的,你想传递元素本身,因为你将它用作sender.checked.

只需.class从通话中删除 。

如果您尝试在一个组内className完成所有操作,那么在获取其他复选框以选中/取消选中时,您需要在函数中使用:

function checkUncheckAll(sender) {
    var chkElements = document.getElementsByClassName(sender.className);
    // Note ------------------------------------------------^^^^^^^^^^
    for (var i = 0; i < chkElements.length; i++) {
        if (chkElements[i] !== sender) {             // Not strictly necessary I guess, but...
            chkElements[i].checked = sender.checked;
        }
    }
}

假设您在“所有”复选框上只有一个类。

于 2016-06-12T13:50:50.823 回答