jQuery在单击时获取下一个同级隐藏值
我试图在链接的单击事件中获取隐藏字段值。以下是这些元素的位置:jQuery在单击时获取下一个同级隐藏值,jquery,Jquery,我试图在链接的单击事件中获取隐藏字段值。以下是这些元素的位置: <p> <a class="showCommentAttachment cboxElement" href="#">Attachments</a> <input type="hidden" value="13" id="ctl00_ContentPlaceHolder1_lvComment_ctrl3_hfCommentId" name="ctl00$ContentPlace
<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>
大卫查特
2011年9月29日08:12:42下午
这里有一些评论文字。这里有一些评论文字。
以下是jQuery:
$("#divComment a.showCommentAttachment").click(function() {
var nextSibling = $(this).next().find('input:hidden').val();
$("#showCommentId").html(nextSibling + "<-- Here's the comment ID ");
});
$(“#divComment a.showcomentattachment”)。单击(函数(){
var nextSibling=$(this.next().find('input:hidden').val();
$(“#showcomentId”).html(nextSibling+”您应该简单地使用$(this).next()
,因为下一个元素肯定是隐藏的输入字段。同样使用$(this).next().find()
您正在查询隐藏输入元素的子元素。这是从:
.find()
获取当前匹配元素集中每个元素的子元素,并通过选择器、jQuery对象或元素进行筛选
因此,您需要$(this).next().val()
.next()
为您提供下一个同级项,即输入字段本身。调用.find()
将搜索此字段的子代(没有任何子代)
您只需要$(this).next().val()
请尝试:
$(this).next().val();
或
$("#divComment a.showCommentAttachment").next().val();