简体   繁体   English

使用jQuery检查和取消选中具有动态ID和类的复选框

[英]Checking and unchecking checkboxes with dynamic ids and class using jquery

I have a grid that has grouping ie the grid shows products of group. 我有一个具有分组的网格,即网格显示分组的产品。 So, the structure has Group and its childs. 因此,该结构具有Group及其子级。 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>

You're calling your function passing in undefined because you're passing this.class : 您正在传递undefined的函数,因为您传递的是this.class

onclick="checkUncheckAll(this.class)"

Elements don't have a class property (they do have className ), and if they did, you dont' want to pass its class , you want to pass the element itself since you're using it as sender.checked . 元素没有class属性(它们确实具有className ),并且如果有,则您不想传递其 ,而是希望传递元素本身,因为您将其用作sender.checked

Just remove the .class from the call. 只需从呼叫中删除.class

If you're trying to do all within a group , then you'll need to use className in the function when getting the other checkboxes to check/uncheck: 如果您要在group中进行所有操作,则在使其他复选框处于选中/取消选中状态时,需要在函数中使用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;
        }
    }
}

That assumes you'll only ever have a single class on the "all" checkboxes. 假设您在“所有”复选框上只能有一个类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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