我的网站上有一个评论部分,它使用jQuery在不重新加载页面的情况下动画评论,以及删除评论。
现在,我有它,所以当你写评论时,它会将它发送到外部JS,然后发送到它处理的php文件。如果成功,它会将评论附加到评论部分。我的删除操作:jQuery删除我在mysql数据库中的注释ID。
所以我的问题是我想通过jQuery添加删除按钮,并以某种方式回调javascript文件并告诉它id,以便删除按钮知道要放入表单的ID,以便删除文件知道要删除什么?
这是我的add_comment
脚本:
$(function() {
$(".commentbutton").click(function() {
$.ajax({
type: "POST",
url: "http://myflashpics.com/process_addcomment.php",
data: $("#commentform").serialize(),
success: function() {
var theComment = $("#commentsss").val();
var theUserId = $("#user_id_two").val();
var thePhotoId = $("#photo_id").val();
var theProfilePicture = $("#user_profile_picture").val();
var theUsername = $("#user_username").val();
// Get new HTML data
var html = "<div class='comment_odd'><img src='" + theProfilePicture + "' class='comment_thumbnail'/><div class='comment_username'>" + theUsername + "</div><div class='comment_text'>" + theComment + "</div></div>";
// Append, then fade in
$(html).hide().appendTo(thecommentsdisplay).fadeIn(500);
}
});
return false;
});
});
提前致谢!
Coulton
编辑1:
这是我的评论表(只是为了澄清):
user_id_two (the user's ID)
commentsss (comments field)
photo_id (the id of the photo being commented on)
user_profile_picture (profile to display on the user's profile picture in the banner)
user_username (username of the user commenting)
这里也是我的删除按钮表单:
<form method='post' action='' name='deleteform' id='deleteform'>
<input type='hidden' name='userid' value='<?php echo "$userid_session"; ?>' />
<input type='hidden' name='userpass' value='<?php echo "$password_session"; ?>' />
<input type='hidden' name='pictureid' id='pictureid' value='<?php echo "$picture_id"; ?>' />
<input type='hidden' name='profilepictureid' id='profilepictureid' value='<?php echo "$user_profile_picture_id"; ?>' />
</form>
最后我的DELETE COMMENT jQuery:
$(function() {
$(".commentdeletebutton").click(function() {
var className = $(this).attr('class');
var theID = className.replace(/delete_button commentdeletebutton delete_(\d+)/, "$1");
commentDone = "#comment_" + theID;
formDone = "#commentdeleteform_" + theID;
$.ajax({
type: "POST",
url: "http://myflashpics.com/process_deletecomment.php",
data: $(formDone).serialize(),
success: function() {
$(commentDone).hide();
}
});
return false;
});
});
答案 0 :(得分:1)
如果您添加了要删除的链接,则可以在href中插入要删除的完整网址,即http://myflashpics.com/process_removecomment.php?id=xx
。
因此,您可以将click事件绑定到该链接,并使用$(this).attr('href')
获取正确的URL并使用ajax进行删除。
编辑更完整的样本:
<? [[Loop your recordset]] { ?>
<div class="comment">
<a href="http://myflashpics.com/process_deletecomment.php?id=<?=$id?>">Delete</a>
<div class="comment">
[[Comment content...]]
</div>
</div>
<? } ?>
<script>
$(function() {
$(".commentdeletebutton").click(function() {
$this = $(this);
$.ajax({
url: $this.attr('href'),
success: function(data) {
$this.parent().slideUp('fast', function() {
$this.parent().remove();
});
}
});
return false;
});
});
</script>