2013-05-17 23 views
0

禁用只复选框我要禁用所有checkboxes这是在同一个div当第一checkbox被选中,如果选中再次启用。这是在同一个div,而不是所有

,其工作对我来说check here

$('.cdls_content_wrapper input:checkbox:first').click(function() { 
    alert('HI'); 
    $('.cdls_content_wrapper').find(':checkbox').slice(1).each(function() { 
     if ($('.cdls_content_wrapper input:checkbox:first').is(':checked')) { 
      $(this).attr("disabled", true); 
     } else { 
      $(this).attr("disabled", false); 
     } 
    }); 
}); 

但问题是,当我有另一个相同的格,然后单击该分区第一checkbox它做都一样。

我想让我的功能更普遍的,而不是具体到只有一个格。

+0


只需粘贴吗?也是为什么你使用复选框以'标签=“”'当你可以写在'检查框标题',你也有很多未闭合的元素 – Epsil0neR

+0

我的第一个div复制到只检查这一点。当然,所有其他div元素都有不同的ID,但我不能使用ID,因为它们是从asp.net – rahularyansharma

+0

中的服务器控件自动生成的,但是您可以在asp.net中自定义该控件,对吗? – Epsil0neR

回答

3

尝试

$('.cdls_content_wrapper').find(':checkbox:first').click(function() { 
    $(this).closest('.cdls_content_wrapper').find(':checkbox').not(this).attr('disabled', this.checked) 
}); 

演示:Fiddle

+0

IN都检查其禁用相同的复选框。 – rahularyansharma

+0

@rahularyansharma查看更新 –

+0

尝试点击第二个全部而不是第二个所有复选框选中。 – rahularyansharma

0

只需推动功能出了处理程序,如:

function DoWork(TheDiv) { 

    var IsFirstChecked; 

    TheDiv.find(':checkbox').each(function() { 

     if (typeof(IsFirstChecked) !== 'boolean') { 
       IsFirstChecked = $(this).prop('checked'); 
     } else { 
       $(this).prop('disabled', IsFirstChecked); 
     } 
    }); 
} 

然后从任何地方调用它:

$('.cdls_content_wrapper').find('input').click(function() { 
    DoWork($(this).closest('table')); 
}); 

$('.SomeOtherClass').find('input').click(function() { 
    DoWork($(this).closest('table')); 
}); 

这里的工作jsFiddle

而且,按照jQuery的documentation,使用.prop()是一种更好的方式去比使用.attr()

-1

你必须在这两个div的使用不同类的复选框

+0

这种建议的任何理由? – rahularyansharma

+0

因为您正在使用类来完成此操作,并且您的所有复选框都具有相同的类 –

2

您可以添加一个classall复选框,这样来做:

$('.cdls_content_wrapper input:checkbox.all').click(function(){ 
    if($(this).is(':checked')){ 
     $('input:checkbox',$(this).closest('tr').siblings()).attr("disabled", true); 
    }else{ 
     $('input:checkbox',$(this).closest('tr').siblings()).attr("disabled", false); 
    } 
}); 

样品fiddle..

0

对不起队友我钉就这一次!为什么你用相同的ID对第二个div的元素

$('.cdls_content_wrapper input:checkbox:first').click(function() { 
    alert('HI'); 
    $(this).parent().parent().parent().find(':checkbox').slice(1).each(function() { 
     if ($('.cdls_content_wrapper input:checkbox:first').is(':checked')) { 
      $(this).attr("disabled", true); 
     } else { 
      $(this).attr("disabled", false); 
     } 
    }); 
}); 
相关问题