2012-04-19 158 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