2011-09-30 59 views
2

我試圖獲得鏈接的點擊事件隱藏的字段值。在此處,這些元素是:jQuery獲得點擊下一個兄弟隱藏值

<p> 
    <a class="showCommentAttachment cboxElement" href="#">Attachments</a> 
    <input type="hidden" value="13" id="ctl00_ContentPlaceHolder1_lvComment_ctrl3_hfCommentId" name="ctl00$ContentPlaceHolder1$lvComment$ctrl3$hfCommentId"> 
</p> 

這裏完整的加價:

<div id="divComment" class="comment-text"> 
    <div class="comment-author"> 
     David Chart 
    </div> 
    <span class="comment-date-time">Sep 29, 2011 08:12:42 PM</span> 
    <p> 
     Some comment text goes here. Some comment text goes here. 

    </p> 
    <p> 
     <a class="showCommentAttachment cboxElement" href="#">Attachments</a> 
     <input type="hidden" value="13" id="ctl00_ContentPlaceHolder1_lvComment_ctrl3_hfCommentId" name="ctl00$ContentPlaceHolder1$lvComment$ctrl3$hfCommentId"> 
    </p> 
</div> 

這裏是jQuery的:

$("#divComment a.showCommentAttachment").click(function() { 

    var nextSibling = $(this).next().find('input:hidden').val(); 

    $("#showCommentId").html(nextSibling + "<-- Here's the comment ID "); 

}); 

什麼我回來從nextSibling是不確定的。我嘗試使用nexAll這樣的

var nextSibling = $(this).nextAll()。find('input:hidden')。val();

仍然未定義。

謝謝。

回答

5

您應該只使用$(this).next(),因爲下一個元素肯定是隱藏的輸入字段。還有$(this).next().find()您正在查詢隱藏的輸入元素的子元素。這是從jQuery API documentation

.find()

獲取每個元素的後代在當前匹配的元素,通過一個選擇器,jQuery對象,或元件過濾。

因此您需要$(this).next().val()

3

.next()給你下一個兄弟姐妹,這是輸入字段本身。然後調用.find()然後搜索該字段的後代(沒有任何)。

你只是想$(this).next().val()

0

請嘗試:

$(this).next().val(); 

$("#divComment a.showCommentAttachment").next().val(); 
相關問題