繁体   English   中英

如何选中和取消选中WebGrid列中的所有复选框?

[英]How to check and uncheck all checkboxes in WebGrid column?

是否有任何原因为什么无法检查和取消选中复选框? 如果我没有触摸任何复选框,它将在按钮单击事件中选中并取消选中所有复选框。 如果我手动选中一个框,此框从此以后将不会被修改,但是其余框将继续被修改。

我该如何解决这个问题?

function checkAll() {
  $('#grid tbody tr').each(function () {
    if ($(this).find("td input[type=checkBox]").is(":checked")) {
      $(this).find("td input[type=checkBox]").removeAttr("checked");
      //$(this).find("td input[type=checkBox]").attr("checked", false); //also tried this
    }
    else {
      $(this).find("td input[type=checkBox]").attr("checked", true);
    }
  });
}

更新1:

我做了额外的循环来遍历每个复选框,但仍然得到相同的结果。 这是更新的代码:

function checkAll() {
    var count = $('#grid tbody tr').length;
    $('#grid tbody tr').each(function () {
        $(this).find("td input[type=checkBox]").each(function () {
            if ($(this).is(":checked")) {
                $(this).attr("checked", false);
            }
            else {
                $(this).attr("checked", true);
            }
        })
    });
}

在此处输入图片说明

根据https://api.jquery.com/attr/的说明,您不应使用attr方法来检查和取消选中复选框,因为它指向defaultChecked属性,并且应仅用于设置复选框的初始值。

可能是您遇到问题的原因,请尝试使用prop()而不是attr()( https://learn.jquery.com/using-jquery-core/faq/how-do-i-check-uncheck-a -复选框输入或单选按钮/

暂无
暂无

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

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