2011-12-30 124 views
0

我有一个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> 

回答

0

相反,电线了在提交你可以做的它在表单上,​​只需将提交按钮更改为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/