我有一些jQuery變量,試圖獲取附近元素的值,但他們不工作。我認爲最接近()是正確的選擇。Jquery樹遍歷不選擇
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>
什麼是.answerContainer?它不在你的html中。我懷疑你實際上是在尋找$(this).find(「。answer_id」) – Archer 2012-01-12 18:04:50
你的標記似乎不完整,你能提供包含.answer_id和.editanser的完整標記嗎? – 2012-01-12 18:05:07