gpt4 book ai didi

javascript - jquery .click() 事件问题(django)

转载 作者:行者123 更新时间:2023-11-30 17:46:03 24 4
gpt4 key购买 nike

我创建了一个生成帖子的 View ,每个帖子都有一个 comments_set,它生成用户发表的所有评论。发布新评论时,将执行以下功能。

$("#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 %}

评论.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;
});

最佳答案

移除id并添加类:

<input type="hidden" class="event-id" value="{{ event.id }}">

做这样的事情:

$('.comment-post-button').click(function(){
var $btn = $(this);
var $currentPost = $btn.parents('.post');
var event_id = $currentPost.find('.event-id').val();
//...
});

$currentPost 范围内查找每个元素:而不是这个:

content: document.getElementsByName('comment-post-content')[0].value

这样做:

content: $currentPost.find('input[name="comment-post-content"]').val()

关于javascript - jquery .click() 事件问题(django),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20139524/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com