私は記事のリストを連結しているので、ユーザーはそれぞれのコメントにコメントを付けることができます(そして、コメントは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関数がそれぞれの固有の「提出物」を認識するのですか?これは私が今持っているものです(下記)。しかし、投稿の横にある送信ボタンをクリックするたびに何も起こらず、ページがリロードされ、URLの末尾に "#"が追加されます。私は私のライブ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>
あなたは投稿のリストを持っており、それぞれにこのような別個の機能があります。 –
はい、投稿のリストですが、htmlの
にある関数は1つだけです。 – Jet59blackあなたのセレクタ '$("。submit <?php echo $ post_id;?> ")'が 'post id 'を持っているので、なぜ私はあなたにこの種の関数を書くべきだと思ったのですか?ページ! ;-) –