jQuery选择表行中的所有复选框

时间:2022-01-01 22:18:57

I have a table with multiple checkboxes in each row, and I created some jQuery to check all the chekboxes on a certain row by clicking another checkbox.

我有一个表,每行有多个复选框,我创建了一些jQuery,通过单击另一个复选框来检查某一行上的所有chekbox。

$('table').on('change', '.selectAll', function(e) {
      if(this.checked) {
          $(e.target).closest('tr').find(".aCheckbox").each(function() {
                $(this).attr('checked', true);
          });
      }
      else {
        $(e.target).closest('tr').find(".aCheckbox").each(function() {
                $(this).attr('checked', false);
          });
      }
});

But it will only work twice (i.e: you can check all the boxes, uncheck them, but then after that nothing will happen when I try to check them again)

但它只会工作两次(即:你可以检查所有的方框,取消选中它们,但之后再尝试检查时不会发生任何事情)

When I'm clicking my .selectAll checkbox it is changing the input's html to checked='checked' successfully but no change is visible.

当我点击我的.selectAll复选框时,它正在将输入的html更改为checked ='checked',但没有看到任何更改。

What is the problem here?

这里有什么问题?

Thanks

谢谢

1 个解决方案

#1


4  

You need to change the checked property, not the checked attribute. You also don't need to iterate, so I simplified the function for you:

您需要更改checked属性,而不是checked属性。你也不需要迭代,所以我简化了你的功能:

$('table').on('change', '.selectAll', function (e) {
    $(this).closest('tr').find(".aCheckbox").prop('checked', this.checked);
});

#1


4  

You need to change the checked property, not the checked attribute. You also don't need to iterate, so I simplified the function for you:

您需要更改checked属性,而不是checked属性。你也不需要迭代,所以我简化了你的功能:

$('table').on('change', '.selectAll', function (e) {
    $(this).closest('tr').find(".aCheckbox").prop('checked', this.checked);
});