2014-12-04 64 views
1

enter image description here取消選中分組TD,如果一個「孩子」 TD是未選中

鑑於上面的圖片,如果用戶取消選中複選框#2,我試圖讓它取消選中複選框#1爲好。目前,當我取消選中複選框#2時,我的代碼將取消選中#1和#3。

HTML代碼(消毒和簡化的)

<!--Grouping Row 1--> 
<tr id="group-id-example_insertjobname"> 
<td colspan="11" data-group="-b-insertjobname" data-group-level="0"> 
<input type="checkbox" class="check" name="" value=""> 
</td> 
</tr> 
<!--Grouping Row 1--> 

<tr role="row" class="odd group-item" data-group="-b-XXX"> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td class="pay"> 
<input type="checkbox" name="PI0" class="Invoice Open" value=""> 
</td> 
</tr> 

<tr role="row" class="group-item" data-group="-b-XXX"> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td class="pay"> 
<input type="checkbox" name="PI1" class="Invoice Open" value=""> 
</td> 
</tr> 

<!--Grouping Row 2--> 
<tr id="group-id-example_insertjobname"> 
<td colspan="11" data-group="-b-insertjobname" data-group-level="0"> 
<input type="checkbox" class="check" name="" value=""> 
</td> 
</tr> 
<!--Grouping Row 2--> 

<tr role="row" class="odd group-item" data-group="-b-XXX"> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td class="pay"> 
<input type="checkbox" name="PI3" class="Invoice Open" value=""> 
</td> 
</tr> 

<tr role="row" class="group-item" data-group="-b-XXX"> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td></td> 
<td class="pay"> 
<input type="checkbox" name="PI4" class="Invoice Open" value=""> 
</td> 
</tr> 

jQuery函數

$(function() { 
    $('.pay input[type="checkbox"]').on('change', function() { 
     if ($('this:checkbox:not(:checked)')) { 
      $('.check').prop('checked', false); 
     } 
    }); 
}); 

回答

1

這種嘗試應該爲你工作:

$(function() { 
    $('.pay input[type="checkbox"]').on('change', function() {   
     if (!$(this).prop('checked')) { 
      $(this).parent().parent().prevAll('tr').not('.group-item').first().find('.check').prop('checked', false); 
     } 
    }); 
}); 

由於$('.pay input[type="checkbox"]')不是一個孩子$('.check')你需要使用parent().parent()去同級別其他tr元素,prevAll('tr').not('.group-item')讓所有前面的分組tr元素,得到與first()找到的第一個,那麼你必須最終找到與$('.check')這應該是你正在尋找的輸入。

+0

謝謝,Sbonkosky,我明白你想要做什麼,但是我仍然沒有找到最近的(或其他).check複選框來取消選中「子」複選框未選中時的選擇。 – dentalhero 2014-12-04 17:50:07

+0

@dentalhero我編輯了我認爲應該可以工作的答案。我忽略了'.check'元素在'tr td'裏面。 – sbonkosky 2014-12-04 18:04:54

+0

謝謝!這現在起作用了,我接受了你的答案。非常感激! – dentalhero 2014-12-04 18:08:16

0

$(this).prevAll().find('.check').prop('checked', false); 
+0

謝謝,theLibertine,但是這並沒有取消選中最近的複選框與一類支票(或任何與此類檢查)。 – dentalhero 2014-12-04 17:25:56

相關問題