问题的本质是我如何选择这个评论的选择器并改变它的内容,因为有很多评论
我的脚本:
$(document).on('click', "#like", function(e){
console.log("Click like")
e.preventDefault()
var id = $(this).attr('data-id')
var like = $(this).text()
const csrftoken = getCookie('csrftoken');
data = {
id : id,
csrfmiddlewaretoken : csrftoken
}
$.ajax({
method: "POST",
data : data,
url : "{% url 'comment-like' video.slug %}",
success : function(data){
if(data['like'] == 'ok') {
console.log("Like now")
console.log(like)
like++
console.log(like)
$(this).innerHTML = like
}
if(data['like'] == 'no') {
console.log("You voted")
console.log(like)
like++
console.log(like)
$(this).innerHTML = like
}
console.log(data)
},
error: function(error) {
console.log(error)
}
})
})
使用脚本处理的评论阻止:
<div class="comments">
<p>Комментарии:</p>
{% for comment in comments %}
<div class="comment">
{{comment.nickname}} | {{comment.timestamp}}
{% if comment.is_child %}
<div style="background-color: black; width: 300px;">
<p>Ответ: {{comment.parent.nickname}}</p>
<p>Комментарий: "{{comment.parent.text}}"</p>
</div>
{% endif %}
<p>{{comment.text}}</p>
<p><a href="#" id="like" data-id="{{comment.id}}"><i class="far fa-heart"></i>{{comment.get_comment_likes}}</a></p>
<button style="background-color: black;" class="reply" data-id="{{comment.id}}" data-parent={{comment.get_parent}}>Ответить</button>
<form action="" method="POST" class="comment-form" id="form-{{comment.id}}" style="display:none;">
<textarea type="text" name="comment-text">
</textarea>
<br>
<input type="submit" class="btn submit-reply" data-id="{{comment.id}}" data-submit-reply="{{comment.get_parent}}" value="Отправить"/>
</form>
</div>
{% endfor %}
<div>
<form action="{% url 'create-comment' video.slug %}" method="POST" >
{% csrf_token %}
{{ comment_form.as_table }}
<input type="submit" class="btn" value="Отправить"/>
</form>
</div>
具体来说,这条线不起作用:
$(this).innerHTML = like
我通过data-id属性制作了一个选择器并传递了变量
var id = $(this).attr('data-id'):