评论系统 - 在连接上

时间:2012-04-19 16:23:58

标签: jquery concatenation

我已经连接了帖子列表,用户可以对每个帖子进行评论(评论会通过jquery获得实时更新)。我决定,因为我正在为每个帖子使用一个唯一的变量,我可以将其添加到更新div的id的末尾,并将其添加到提交按钮的类的末尾,如下所示:< / p>

$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>

1 个答案:

答案 0 :(得分:0)

尝试将<input type="submit" class="submit' . $post_id . '" value="Comment " />替换为<input type="button" class="submit' . $post_id . '" value="Comment " />,我认为return false不会停止在此处提交表单!但无法测试这个!

相关问题