-1
我在循環中打印下面的HTML代碼片段。使用parent()在jQuery中獲取值
<div class = "commentbox">
<input type="hidden" name="postid" value="${p.postid}" />
<input type="hidden" name="username" value="${username}" />
<input type="hidden" name="source" value="user_home" />
<textarea name="comment" cols="40" rows="1" class="add_comment"
placeholder="Add a comment..."></textarea>
<div class="post_con">
<button type="submit" class="postcomment">Post</button>
</div>
</div>
我有這樣的jQuery的片而言在<button class="postcomment">
一下,當我想獲得的<input name="postid">
值。
$(".postcomment").click(function(){
var parent = $(this).parent();
var postid = parent.find(".postid").val();
console.log(postid);
});
但是,它記錄undefined
。我怎樣才能得到postid
的價值?
我沒有看到postid類。你的意思是reviewid? – Evus
@Evus嘿!犯了一個錯誤。再次檢查出來。 – ProgramAllDay
我知道這是回答,但你的jq會工作,如果你加強了一個父母...... var parent = $(this).parent()。parent();''postcomment'是在一個div parent1),那麼這是在另一個div - 'commentbox'(parent2)中。 - 你正試圖在'post_con' div中找到()'''nearest()'更好:) – Scott