我有一个stackoverflow风格的评论系统,其中有一个可变数量的帖子(“答案”)在一个页面上,人们可以评论。我正在尝试使用jquery来获取用户评论的唯一选择器,将其提交到mysql数据库中并显示它,所有这些都不需要刷新。问题是我不知道如何选择单个评论,因为评论需要有一个唯一的选择器,而现在它们都在同一个类(.commentBox)下。
JQUERY:
<script type='text/javascript'>
$('document').ready(function(){
$('.submitCommentBox').click(function(){
var comment = $(' //idk ').valu();
var questionid = $(' //idk ').val();
var answerid=$(' //idk ').val();
$.post('../comment.php',
{
comment: comment,
questionid: questionid,
answerid: answerid,
},
function(response){
$('#commentContainer').load('../writecomment.php');
});
}):
});
</script>HTML (这是在while循环中,根据帖子的数量进行多次回显):
<div class='answerContainer'>
<p name='singleAnswer' value='$answerid[$f]'>$answer[$f]</p>
<p>$answeruser[$f]</p>
<p> $time[$f] seconds</p>
<p> $answerrating[$f]</p>
<form id='form$c'>
<div id='commentContainer'></div>
<textarea class='commentBox' placeholder='...comment' wrap='soft' name='comment' value='$c'></textarea>
<input type='button' value='submit' class='submitCommentBox'>
</form>
</div>发布于 2012-01-13 06:46:30
您可以在表单上执行此操作,只需将submit按钮更改为type=" submit“,而不是在提交上执行此操作。
$('.addCommentForm').submit(function(){
$.post({
type: 'POST',
url: '../comment.php',
data: $(this).serialize(), // $(this) referring to the source form that triggered the submit.
success: function(data, textStatus, jqXHR) {
// Do whatever like add the display only equivalent of the comment
// that was just submitted if successful.
}
});
return false; // Prevent the form from actually submitting the old fashion way.
});我会参考jquery api来了解更多细节。http://api.jquery.com/jQuery.post/
https://stackoverflow.com/questions/8680425
复制相似问题