【发布时间】:2013-11-22 07:38:05
【问题描述】:
我创建了一个生成帖子的视图,每个帖子都有一个 cmets_set,它生成用户创建的所有 cmets。当发布新评论时,将执行以下函数。
$("#comment-post-button").click(function(){
var event_id = document.getElementById('event-id').value;
var url= '/post-comments/'+event_id +'/';
var data = {csrfmiddlewaretoken: document.getElementsByName('csrfmiddlewaretoken')[0].value,
content:document.getElementsByName('comment-post-content')[0].value
}
$.post(url , data, function(){
$("#refresh-comments").load("/comments/" + event_id + '/', function(){
$("#comment-post-content").val("");
});
});
$("#comment-post-content").val("");
return false;
});
问题是该页面包含多个帖子,并且每个评论提交按钮具有相同的 id “comment-post-button”。所以上面的函数只对顶帖有效,对其余的无效。我可以看到问题是什么,但不知道如何解决。请帮忙。
这里是html标记:
{% for event in events %}
<div class="post">
<div class="post-right">
<div class="post-author"><a href="/{{ event.author.username }}/">{{ event.author.first_name }}</a></div>
<div class="post-content">{{ event.description }}</div>
<div class="post-bar">
<div class="post-likes">{{ event.up_votes }}<a href="#"><img src="/site-media/static/images/like.png" /></a></div>
<div class="post-dislikes">{{ event.down_votes }}<a href="#"><img src="/site-media/static/images/dislike.png" /></a></div>
<div class="post-timestamp">{{ event.pub_date }}</div>
<a href="#" class="post-comment-link">Comment</a>
</div>
<div class="post-comment">
<form method="post" action="/post-comments/{{ event.id }}/">
{% csrf_token %}
<input type="text" id="comment-post-content" name="comment-post-content" maxlength="200" placeholder="Add a comment..." />
<input type="hidden" id="event-id" value="{{ event.id }}">
<input type="submit" id="comment-post-button" class="comment-post-button" value="Post comment" />
</form>
</div>
<div id="refresh-comments" class="comments">
{% include "comments.html" %}
</div>
</div>
<div class="post-left">
<a href="#"><img src="../FestJanta/site-media/static/images/post.jpg" /></a>
</div>
</div>
{% endfor %}
cmets.html:
{% for comment in event.comment_set.all|slice:"3" %}
<div class="comments-right">
<a href="/{{ name }}/" class="first_name">{{ comment.author.first_name }}</a>
{{ comment.content }}<br>
<div class="comment-timestamp">{{ comment.pub_date }}</div>
</div>
<div class="comments-left"><a href="#"><img src="../FestJanta/site-media/static/images/comment.jpg" /></a></div>
{% endfor %}
最终工作解决方案:
$(".comment-post-button").click(function(){
var btn = $(this);
var currentPost = btn.parents('.post');
var event_id = currentPost.find('.event-id').val();
var url= '/post-comments/'+event_id +'/';
var data = {csrfmiddlewaretoken: document.getElementsByName('csrfmiddlewaretoken')[0].value,
content:currentPost.find('input[name="comment-post-content"]').val()
}
$.post(url , data, function(){
$(currentPost.find('.refresh-comments')).load("/comments/" + event_id + '/', function(){
$(currentPost.find('.comment-post-content')).val("");
});
});
return false;
});
【问题讨论】:
-
您可以为所有的 Post 按钮提供相同的类,然后按类访问它们
-
@AjinderSingh 但是,这将如何解决问题。我需要唯一标识一个发布按钮
-
@karaxuna 你能详细说明一下吗?
-
@AjinderSingh 我试过这样做,但这会导致将所有 cmets 发布到第一个事件。
-
@Monique - 您可以使用 $(this) 访问当前单击的按钮
标签: javascript jquery python html django