使用Jquery检查/取消选中所有CheckBoxes [英] Check/Uncheck All CheckBoxes using Jquery

查看:133
本文介绍了使用Jquery检查/取消选中所有CheckBoxes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何选中/取消选中复选框(Child)使用单个(Parent)复选框,反之亦然?

当我使用普通的Html复选框时代码正常工作。

其中Child和Parent是复选框的类。



  //  添加多项检查/取消选中功能 
$( 。Parent)。click(function(){
$( < span class =code-string>。Child)。attr( checked已检查);
});

// 如果选中所有子复选框,请选中Parent复选框
// and viceversa
$( 。Child)。click(function(){

if ($( 。Child)。length == $( 。Child:选中)。length){
$( 。Parent)。attr( 已选中 已选中);
} else {
$( 。Parent)。祛瘀eAttr( checked);
}
});







但是当我使用asp时。 net复选框此代码无效。

然后我修改了以下代码。

  //  添加多项检查/取消选中功能 
// 父类只有一个复选框
$( 。父:第一个孩子)。点击(
function(){
$( 。Child)。find( :checkbox)。attr( 选中已检查);
});

// 如果选中所有子复选框,请选中Parent复选框
// and viceversa
$( 。Child)。find( :复选框)。点击(function(){

if ($( 。Child)。length == $( 。子项:选中)。length){
$( .Parent)。find( :checkbox)。attr(< span class =code-string> 选中 检查);
} else {
$( .Parent)。find( :checkbox)。removeAttr(< span class =code-string>
checked);
}
});









这是因为跨度渲染问题吗?

如何解决这个问题?



先谢谢。

解决方案

。Parent)。click(function() {


。Child)。attr(< span class =code-string> 选中已检查);
});

// 如果选中所有子复选框,请选中Parent复选框
// and viceversa


。Child)。click(function(){

如果

Hi,
How to check/uncheck checkboxes(Child) using a single(Parent) checkbox or vice-versa?
When i am using normal Html checkbox following code is working.
where Child and Parent are classes given to checkboxes.

// add multiple Check / Uncheck functionality
    $(".Parent").click(function () {
          $(".Child").attr("checked", this.checked);
    });

    // if all child checkbox are selected, check the Parent checkbox
    // and viceversa
    $(".Child").click(function(){

        if($(".Child").length == $(".Child:checked").length) {
            $(".Parent").attr("checked", "checked");
        } else {
            $(".Parent").removeAttr("checked");
        }
  });




But when i am using asp.net checkboxes this code is not working.
then i have modified the code as below.

// add multiple Check / Uncheck functionality
  //Parent class is having only one checkbox 
$(".Parent:first-child").click(
function() {
          $(".Child").find(":checkbox").attr("checked", this.checked);
    });

    // if all child checkbox are selected, check the Parent checkbox
    // and viceversa
    $(".Child").find(":checkbox").click(function(){

        if($(".Child").length == $(".Child:checked").length) {
            $(".Parent").find(":checkbox").attr("checked", "checked");
        } else {
            $(".Parent").find(":checkbox").removeAttr("checked");
        }
  });





Is this Because of Span Rendering Problem?
How to Solve this Problem?

Thanks in Advance.

解决方案

(".Parent").click(function () {


(".Child").attr("checked", this.checked); }); // if all child checkbox are selected, check the Parent checkbox // and viceversa


(".Child").click(function(){ if(


这篇关于使用Jquery检查/取消选中所有CheckBoxes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆