2014-04-04 105 views
0

folliwing小提琴是好的,阅读下面我需要改变它。如何勾选其他部分的所有复选框,方法是勾选当前部分中的复选框?

http://jsfiddle.net/V5SSM/14/

您点击☑分段1.1,然后将所有其他获得点击:

  • ☑小节2.1
  • ☑小节2.2
  • ☑2.3小节
  • ☑分段2.4
  • ☑3.1小节
  • ☑分段3.2
  • ☑分段3.3
  • ☑3.4小节

下面是HTML树:

第一节

Subsection 1.1 
Subsection 1.2 
Subsection 1.3 
Subsection 1.4 

第2节

Subsection 2.1 
Subsection 2.2 
Subsection 2.3 
Subsection 2.4 

第3

Subsection 3.1 
Subsection 3.2 
Subsection 3.3 
Subsection 3.4 

更多详细信息:

你可以看到,目前的jsfiddle示例工作。它用于勾选当前部分的所有复选框,同时勾选当前部分中的一个复选框。我希望它的工作原理相同,但不要勾选它自己的部分的复选框,而是勾选其他部分的所有其他复选框。

举例:我选择“1.1”节中的所有其他节不是第1节。 “节1.1”应该选择所有其他节:2.1节,2.2节,2.3节,2.4节,3.1节,3.2节,第3.3,3.4小节

+0

我不明白你想做什么,你可以更具体吗? – Stphane

+0

您点击☑分段1.1,然后将所有其他获得点击: ☑小节2.1 ☑小节2.2 ☑2.3小节 ☑小节2.4 ☑小节3.1 ☑小节3.2 ☑小节3.3 ☑3.4小节 – Tudor

+0

奥基,我正在更新你的小提琴......如果我点击第1,2小节怎么办? – Stphane

回答

1

您可以处理单击使用事件委托机制通过对共同祖先连接处理程序的所有复选框:

var type = 'input[type="checkbox"]'; 
$('#selectAllButton').on('click', function() { 
    $(type).prop('checked', true).closest('label').addClass('c_on'); 
}); 
$('#selectNoneButton').on('click', function() { 
    $(type).prop('checked', false).closest('label').removeClass('c_on'); 
}); 

// event Delegation 
$('#docbuilder').click(function(e){ 
    var $t = $(this), 
     $tar = $(e.target); // The node that has been clicked  
    // If checkbox 
    if($tar.is(type)){ 
     var cbRemaining = $tar.closest('blockquote').find(type+':checked').length, /* counting how many checked checkboxes remain inside the current section */ 
      $otherCb = $t.children(':nth-child(n+3)').not($tar.closest('.document')[0]).find(type), /* Retrieve all checkboxes except the ones that belong to the current section */ 
      state = $tar.prop('checked'); // get the checked property of the checkbox beiing clicked 
     // If subsection checkbox 
     if($tar.closest('.subsection').length){ 
      if(!cbRemaining){ 
       $tar.closest('blockquote').prev().find(type).prop('checked', false); 
      } 
      if(!cbRemaining || cbRemaining==1 && state){ 
       $otherCb.prop('checked', state); 
      } 
      $tar.parent()[(state ? 'addClass':'removeClass')]('c_on'); // should we add or remove the color class on the checkbox label 
     // If section checkbox 
     } else if($tar.closest('.section').length){ 
      if(state) $otherCb.prop('checked', false); 
      $tar.parent().siblings().find(type).prop('checked', state) 
      .parent()[(state ? 'addClass':'removeClass')]('c_on'); 

     } 
    } 
}); 

您可能需要一点相应地修改这个代码的一些限制尚未考虑在内。

+0

Concider upvoting,并选择它作为你的解决方案,如果这能解决你的问题';)' – Stphane

+0

我不能调高... http://jsfiddle.net/V5SSM/28/ – Tudor

+0

是的,我想,但你可以选择我的答案作为解决方案,因为我解决了你的问题,并花时间在它上面'^^' – Stphane