2012-04-19 155 views
0

我已經連接了職位列表,用戶可以對每個職位發表評論(評論通過jquery獲得實時更新)。我決定,因爲我爲每個帖子使用了一個唯一的變量,所以我可以將它粘貼到更新div的id的末尾,並將其粘貼到提交按鈕類的末尾,如下所示:評論系統 - 級聯

$my_list .= ' 
    <form action="#" method="post"> 
     <textarea class="commentbox" name="comment" id="comment"></textarea> 
     <input type="hidden" name="post_id" id="post_id" value=' . $post_id . ' /> 
     <input type="hidden" name="member_id" id="member_id" value=' . $id . ' /> 
     <input type="submit" class="submit' . $post_id . '" value="Comment " /> 
    </form> 
    ' . $comment_list . ' // existing comments queried from the db 
    <ol id="update' . $post_id . '"></ol> // jquery-ed live update comments 
'; 
?> 

關於上述的一切看起來很好(即每個提交按鈕獲取一個獨特的類,每個更新div得到一個唯一的ID)。

所以我現在遇到的問題是:我如何讓我的js函數識別每個獨特的「提交」?這是我現在的(下面)。但是,無論何時點擊帖子旁邊的提交按鈕,都不會發生任何事情,我的頁面會重新加載,並且會將「#」添加到網址的末尾。我檢查了我的實時http頭文件,它看起來像是發佈了正確的唯一$ post_id,但我相信它停止在js函數中。

<head> 
<script type="text/javascript"> 
$(function() { 
    $(".submit<?php echo $post_id; ?>").click(function() { 
     var post_id = $("#post_id").val(); // this is the unique id for each story, in a hidden input 
     var member_id = $("#member_id").val(); // this is a member for the commenter, in a hidden input 
     var comment = $("#comment").val(); // this is the comment from the input box 
     var dataString = 'post_id='+ post_id + '&member_id=' + member_id + '&comment=' + comment; 
     if(comment=='') { 
      alert('Please enter a valid comment'); 
     } else { 
      $("#flash").show(); 
      $("#flash").fadeIn(400).html('<span class="loading">Loading Comment...</span>'); 
      $.ajax({ 
       type: "POST", 
       url: "commentajax.php", 
       data: dataString, 
       cache: false, 
       success: function(html){ 
        $("#update<?php echo $post_id; ?>").append(html); 
        $("#update<?php echo $post_id; ?> li:last").fadeIn("slow"); 
        document.getElementById('post_id').value=''; 
        document.getElementById('member_id').value=''; 
        document.getElementById('comment').value=''; 
        $("#comment").focus(); 
        $("#flash").hide(); 
       } 
      }); 
     } 
     return false; 
    }); 
}); 
</script> 
</head> 
+0

只是問,你有一個職位列表,他們每個人都有這樣的單獨功能! – 2012-04-19 16:42:29

+0

是的,它是一個職位列表......但只有一個功能在HTML的。 – Jet59black 2012-04-19 17:27:07

+0

爲什麼我這樣問,因爲你的選擇器'$(「。submit <?php echo $ post_id;?>」)'有一個'post id',我只是覺得你要寫這樣的函數給每一篇文章頁! ;-) – 2012-04-20 02:29:35

回答

0

嘗試用<input type="button" class="submit' . $post_id . '" value="Comment " />取代<input type="submit" class="submit' . $post_id . '" value="Comment " />,我想return false不會停止的形式提交這裏!但無法測試這個!

+0

我試着說你說的,但不是什麼都沒有發生,頁面提交甚至不發佈。 – Jet59black 2012-04-19 17:30:58

+0

我應該在輸入框中包含一個onclick =「newPost()函數嗎? – Jet59black 2012-04-19 17:52:35

+0

您在這裏沒有使用表單的功能,而是使用AJAX,對嗎?然後刪除表單並使用div。 – 2012-04-20 02:28:57