2011-12-30 10 views
0

私はスタックオーバーフロースタイルのコメントシステムを持っています。ここでは、人がコメントできるページに可変数のポスト(「回答」)があります。私は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

の代わりにあなたが行うことができます提出上のワイヤアップそれはフォーム上にあり、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/