2013-04-29 28 views
0

我想要選擇下一個離輸入標籤更近的div更改。當我運行這個沒有任何反應。我試過了最近的標籤和下一個標籤。如何正確選擇下一個div與jQuery

$("input[id='declined']").change(function(){ 
    $(this).next('div.textarea_container').fadeIn(); 
}); 

HTML:

<div id="gcheckbox"> 
     <input type="radio" id="name10" class="guidelines" name="Confirmed diagnosis of melanoma" value="Accepted"> 
     <input type="radio" class="guidelines no_margin" name="Confirmed diagnosis of melanoma" id="declined" value="Declined"> 
     <label>Confirmed diagnosis of melanoma</label> 
       <div class="textarea_container"> 
       <textarea placeholder="reason" id="notearea0"></textarea> 
       </div> 
    </div> 

我現在已經發布的一個示例文件。 http://jsfiddle.net/dMmuW/

+1

顯示您的HTML。 – Barmar 2013-04-30 00:01:15

+0

'[id ='rejected']'可以在嘶嘶聲選擇器中寫入##拒絕 – Andbdrew 2013-04-30 00:02:55

+0

[doco for next()](http://api.jquery.com/next/)使得它很清楚爲什麼它是該html結構的錯誤方法... – nnnnnn 2013-04-30 00:11:57

回答

2

jQuery的next功能僅適用於相鄰兄弟元素,使用nextAll讓所有同級元素選定一個後篩選出你想要的。

$('#declined').change(function() { 
    $(this).nextAll('div.textarea_container').fadeIn(); 
}); 
+0

這似乎不工作。謝謝你的建議。 – 2013-04-30 00:09:26

+0

最初的答案是在他提供代碼示例之前編寫的;現在更新。 – thefrontender 2013-04-30 00:13:02

+0

+1這是正確的答案。 – Hubro 2013-04-30 00:13:03