Jquery树遍历不选择
我有一些 jquery 变量试图获取附近元素的值,但它们不起作用。我认为closest()是正确的选择器。
JQUERY:
$(".answerContainer").on("click", ".editAnswer", function(e){
e.preventDefault();
var answer_id = $(this).closest('.answer_id').attr('value'); //this doenst work
HTML:
<p name='singleAnswer' class='singleAnswer'>$answer[$f]</p>
<input type='hidden' value='$answerid[$f]' class='answer_id' />
<p class='ratingBox'> $answerrating[$f]</p>
<div class='answerBar'>";
<a href='#' class='upVote vote'>Upvote</a> · <a href='#' class='downVote vote'>Downvote</a> ·
<a class='answerTime'> $difference $periods[$j] ago</a>
· <a href='#' style='color: orange;' class='editAnswer'><b>Edit</b></a>
I have some jquery variables that attempt to get values of nearby elements however they don't work. I thought closest() was the right selector.
JQUERY:
$(".answerContainer").on("click", ".editAnswer", function(e){
e.preventDefault();
var answer_id = $(this).closest('.answer_id').attr('value'); //this doenst work
HTML:
<p name='singleAnswer' class='singleAnswer'>$answer[$f]</p>
<input type='hidden' value='$answerid[$f]' class='answer_id' />
<p class='ratingBox'> $answerrating[$f]</p>
<div class='answerBar'>";
<a href='#' class='upVote vote'>Upvote</a> · <a href='#' class='downVote vote'>Downvote</a> ·
<a class='answerTime'> $difference $periods[$j] ago</a>
· <a href='#' style='color: orange;' class='editAnswer'><b>Edit</b></a>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试一下,也使用
.val()
而不是attr('value')
Try this, also use
.val()
instead ofattr('value')
Closest 假设您要遍历节点的祖先。在这种情况下,请尝试 prev()
Closest assumes you want to traverse through the ancestors of the node. In this case, try prev()