2011-12-30 193 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/