问题
I'm trying to get the hidden field value on click event of a link. Here's where those elements are:
<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>
Here the complete mark-up:
<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>
Here's the jQuery:
$("#divComment a.showCommentAttachment").click(function() {
var nextSibling = $(this).next().find('input:hidden').val();
$("#showCommentId").html(nextSibling + "<-- Here's the comment ID ");
});
what I get back from nextSibling is undefined. I tried with nexAll like this
var nextSibling = $(this).nextAll().find('input:hidden').val();
still got undefined.
Thank you.
回答1:
You should use simply $(this).next()
since the next element is surely the hidden input field. Also with $(this).next().find()
you are querying the children elements of the hidden input element. This is from jQuery API documentation:
.find()
Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.
So you need $(this).next().val()
.
回答2:
.next()
gives you the next sibling which is the input field itself. Calling .find()
is then searching for descendants of this field (there aren't any).
You just want $(this).next().val()
回答3:
please try:
$(this).next().val();
or
$("#divComment a.showCommentAttachment").next().val();
来源:https://stackoverflow.com/questions/7608191/jquery-get-the-next-sibling-hidden-value-on-click