这是一个评论/回复系统。以下代码显示注释的主要注释和回复。 jQuery代码应该选择(段落id="test"
)元素,并在单击回复按钮时将消息附加为回复消息。
jQuery $(this).parents("p").append("hello");
无效。我尝试过使用closest("p")
和find("p")
,但仍然无效。有任何想法吗?
<div class='box'>
<?php
// Comments from database code goes here
?>
</div>
<div class="feedback">
<?php
while ($row = mysqli_fetch_array($query)) {
echo "".$row['Username'].": ";echo $row['replyormsg'];
}
?>
<p id="test"><p>
</div>
<div class="replymsg">
<!-- Reply Form/Textbox -->
<form class=".replyform">
<textarea class="replytext" ></textarea>
<button class="replybutton" data-value="<?php echo $commentid; ?>">Submit</button>
</form>
</div>
$(document).ready(function(){
$(".replybutton").click(function(e){
e.preventDefault();
var id = $(this).data("value");
var replytext = $(this).prev().val();
$(this).parents("p").append("hello");
$("textarea").val("");
});
});
答案 0 :(得分:3)
.replyButton
元素没有父p
元素,因此parents('p')
选择器不返回任何内容。假设您正在寻找p
中的.feedback
元素,您应该使用它:
$('.feedback p').append("hello");
假设您正在使用DOM遍历,因为重复了这个HTML块,您首先应该从id="test"
中删除p
(因为它将被重复,因此无效)。然后,您可以使用以下内容从点击的.feedback p
遍历到最近的.replyButton
:
$(this).closest('.replymsg').prev('.feedback').find('p').text('hello');