大家好,我来找你是因为我有一个小问题,我在ajax中创建了一个动态评论系统,我想编辑每个评论,除了这样做时,它会改变我的上一个而不是单击元素 我想念我想念的东西
我还没有尝试过任何东西,但是我不知道如何做
我的代码: js:
$('#edit-comments').hide();
$('#add-comments').on('click', function (e) {
e.preventDefault();
$.ajax({
url: $(this).attr('action'),
type: 'POST',
data: $('#formsComments').serialize(),
dataType: 'json',
success: function (data) {
$('#comments_commentsContent').val('');
$('#content-comments').append(data.content);
},
error: function (data) {
console.log(data);
if(data.status === 500) {
$('#errors-comments').html('<div class="error">Vous n\'avez pas ajouté de commentaires\n</div>')
}
}
});
});
$('.comments-content').on('click', function (e) {
e.preventDefault();
$('#add-comments').hide();
$('#edit-comments').show();
var idEdit = $(this).attr('id');
var idContent = idEdit.split('-');
var res = $('p#comments-content-'+idContent[2]).text();
console.log(idContent);
$('#comments_commentsContent').val(res);
$('#edit-comments').on('click', function (e) {
e.preventDefault();
$.ajax({
url: '/article/commentaires/'+idContent[2]+'/edit/'+idContent[3],
type: 'POST',
data: $('#formsComments').serialize(),
dataType: 'json',
success: function (data) {
console.log(data);
$('#comments_commentsContent').val('');
$('#add-comments').show();
$('#edit-comments').hide();
$('#comments-content-'+idContent[2]).html(data.content.message);
},
error: function (data) {
console.log(data);
}
});
});
});
我的树枝视图
{% for comment in article.comments %}
<div class="media" id="media">
<div class="media-body">
<div class="media-heading">
<h4>{{ comment.userComments.username }}</h4>
<span class="time">{{ comment.createdAt | date("d/m/Y à H:i") }}</span>
{% if app.user %}
{% if comment.userComments.id == app.user.id %}
<a href="{{ path('front_article_edit_comments', {'id': comment.id, 'user': comment.userComments.id}) }}#comments" class="comments-content" id="edit-comments-{{ comment.id }}-{{ comment.userComments.id }}" >
<i class="fa fa-pencil" aria-hidden="true"></i>
</a>
{% endif %}
{% endif %}
{% if is_granted('ROLE_ADMIN') %}
<a href="{{ path('front_article_delete_comments', {'id': comment.id}) }}#comments" onclick="return confirm('Vous voulez vous vraiment supprimer ce commenaitre ?');">
<i style="padding-left: 10px; color: #CBBBA3;" class="fa fa-times" aria-hidden="true"></i>
</a>
{% endif %}
</div>
<p id="comments-content-{{ comment.id }}">{{ comment.commentsContent|nl2br }}</p>
</div>
</div>
{% endfor %}
新评论:
if($request->isXmlHttpRequest() && $form->isValid()) {
$user = $this->getUser();
$comments->setUserComments($user);
$comments->setArticleComments($articles);
$em->persist($comments);
$em->flush();
$html = $this->renderView('articles/comments.html.twig', ['comment' => $comments]);
return $this->json(['content' => $html]);
}
编辑评论:
if($request->isXmlHttpRequest() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->flush();
return $this->json(['content' => [
'message' => $comments->getCommentsContent(),
'user' => $comments->getUserComments()->getId(),
'id-comments' => $comments->getId(),
]]);
}
当我要编辑第一个注释或第二个注释时,它不起作用。