Jquery获取输入隐藏值不起作用
我试图获取隐藏输入类型的值,但它不起作用。
JQUERY(在脚本标签中):
$('.flagComment').click(function() {
var commentid = $(this).siblings('.commentId').attr('value');
alert(commentid);
});
HTML(在 while 循环中回显几次):
<div class='bar'>
<a href='#' class='flagComment'>Flag</a>
</div>
<input type='hidden' class='commentId' value='testvalue' />
I am trying to get the value of a hidden input type, but it's not working.
JQUERY (in script tags):
$('.flagComment').click(function() {
var commentid = $(this).siblings('.commentId').attr('value');
alert(commentid);
});
HTML (this echos several times in a while loop):
<div class='bar'>
<a href='#' class='flagComment'>Flag</a>
</div>
<input type='hidden' class='commentId' value='testvalue' />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试:
因为隐藏输入是 div 的同级,而不是锚点。
Try:
since the hidden input is a sibling of the div, and not the anchor.
这应该效果更好,因为
.commentId
不是.flagComment
的同级:演示
This should work better, because
.commentId
is not a sibling of.flagComment
:Demo
input 不是此链接的同级。现在它可以工作了:
您也可以尝试使用closest()函数:
input is not a sibling for this link. Now it works:
You can try also with closest() function: