2010-09-30 57 views
29

我有這樣的HTML代碼:jQuery的父()( 「選擇」)

<tr> 
    <td><input type="checkbox" class="chk" /></td> 
    <td><div class="disabled">text to hide 1</div></td> 
    <td><div class="disabled">text to hide 2</div></td> 
</tr> 

我使用jQuery來隱藏所有class="disabled"項目:

$("div.disabled").hide() ; 

我想展示殘疾人的div當我點擊同一行(tr)中的複選框時。 我試過

$("input.chk").click(function(){ 
    $(this).parent().parent().(".disabled").show(); 
}) ; 

但它不起作用。

回答

65

使用.closest().find(),像這樣:

$("input.chk").click(function(){ 
    $(this).closest('tr').find(".disabled").show(); 
}); 

您當前的代碼幾乎的作品,你需要一個.find()不過,像這樣:

$(this).parent().parent().find(".disabled").show(); 

如果你有許多行像這樣,使用.delegate(),像這樣:

$("table").delegate("input.chk", "click", function(){ 
    $(this).closest('tr').find(".disabled").show(); 
}); 

.delegate()而是將一個處理程序綁定到所有input.chk元素的表格,以便向上吸氣。如果你正在尋找啓用/禁用,使用change.toggle()除了上面,像這樣:

$("table").delegate("input.chk", "change", function(){ 
    $(this).closest('tr').find(".disabled").toggle(this.checked); 
}); 

這樣,如果它的檢查,他們表示,如果他們不隱瞞。

+1

.closest()得到過時,在1.8 http://api.jquery.com/closest/(見頁面底部)刪除查找HTTP ://api.jquery.com/parentsUntil/ – 2014-05-19 18:13:34

+0

如果你真的看鏈接,最接近的不會被棄用,只是一個特定的簽名。我沒有看到任何信息表明對父母的偏好超過最接近的。 – Danwizard208 2015-10-26 13:49:59

2

差不多。你只是錯過了find這個詞來表示jQuery's .find() method

$("input.chk").click(function(){ 
    $(this).parent().parent().find(".disabled").show(); 
}) ; 

或者,稍小一點的版本是use .closest()

$("input.chk").click(function(){ 
    $(this).closest('tr').find(".disabled").show(); 
}); 

你也可以use .parents(),儘管你想表明你已經嵌套表的情況下the :first比賽。

$("input.chk").click(function(){ 
    $(this).parents('tr:first').find(".disabled").show(); 
}); 
0

它實際上比這更容易。

$(".disabled").hide(); 
    $(".chk").click(function(){ 
     if($(this).is(":checked")) 
     { 
      $(this).siblings(".disabled").show(); 
     } 
     else 
     { 
      $(this).siblings(".disabled").hide(); 
     } 
    }); 

我甚至增加了一些額外的功能,這樣的事件不只是觸發一次,而是基於該複選框是否被選中或不切換。

+0

元素不是兄弟姐妹,它們在不同的表格單元格中:) *如果它們是,則簡短方法是:'$(「.chk」)。change(function(){$(this).siblings (「.disabled」)。toggle(this.checked);});' – 2010-09-30 18:59:02

+0

我假設他們是兄弟姐妹,因爲他們在共享父級中包含的同一級別上。我正在跟nubbel談話,你說得對,我正在用和

做同樣的事情。 – Eric2010-09-30 19:42:03

0

而且那就更簡單了:使用find()closest()

$(".disabled").hide(); 
$(".chk").click(function() { 
    $(this).siblings(".disabled").toggle(); 
});​ 

:-)

+0

當我評論其他答案(除了這個不考慮初始狀態),'.disabled'元素不是兄弟姐妹:) – 2010-09-30 19:03:38

+0

我是這麼想的,並且即將對Eric進行評論,但我試過了:http ://jsfiddle.net/jpQcu/。似乎'$ .siblings()'返回樹中相同深度的元素。我認爲'$ .nextAll()'就是我們的意思。 – nubbel 2010-09-30 19:18:15

+0

@nubbel - '.nextAll()'也是*僅用於兄弟姐妹:) – 2010-09-30 19:27:28

3

是絕對正確的程序。有相同的寫作風格。代碼片段在這裏。

$("input.chk").click(function() { 
     var th = $(this); 
     $(".disabled", th.parent().parent()).show(); 
}); 
+0

你有一個語法錯誤,在第3行:) – 2011-04-11 12:21:48

+0

@Shrikant Sharat謝謝。我糾正了我的答案:)。 – Anji 2011-04-11 13:30:57

0

現在,它的工作原理:http://jsfiddle.net/WAQBj/2/

$(".disabled").hide(); 
$(".chk").click(function() { 
    $(this).closest('tr').find(".disabled").toggle(); 
});​