Javascript 使用ajax和django删除for循环中的项
正在尝试使用“单击并淡出”引导程序删除帖子/评论。我写了一些代码,但它不工作。我觉得我错过了这么简单的事情。任何指导或帮助都将不胜感激。 url.py 模板Javascript 使用ajax和django删除for循环中的项,javascript,jquery,ajax,django,Javascript,Jquery,Ajax,Django,正在尝试使用“单击并淡出”引导程序删除帖子/评论。我写了一些代码,但它不工作。我觉得我错过了这么简单的事情。任何指导或帮助都将不胜感激。 url.py 模板 {% for warns in warn.warnings.all %} <div class="warning-list col-md-4"> {# <a href="{% url 'delete' pk=warns.pk %}"> #}
{% for warns in warn.warnings.all %}
<div class="warning-list col-md-4">
{# <a href="{% url 'delete' pk=warns.pk %}"> #}
<button type="button" class="close">×</button>
{# </a> #}
<div class="publish-date"><strong>Warned:</strong> {{ warns.publish_date }}</div>
<div class="posted-by"><strong>Warned By: </strong> {{ warns.author }}</div>
<div class="warningtype"><strong>Warning Type: </strong>{{ warns.WarningType }}</div>
<div class="description">
<strong>Description:</strong> {{ warns.descript }}
</div>
{% endfor %}
main.js如下所示
$(".close").click(function () {
var close=$(this).parent();
if (confirm("Are you sure you want to delete this?")){
$.ajax({
type: "DELETE",
url: $(this).data('url'),
beforeSend: function(xhr) {
xhr.setRequestHeader("X-CSRFToken", getCookie("csrftoken"));
},
success: function() {
close.fadeOut(1000);
}
});
}
});
{% for warns in warn.warnings.all %}
<div class="warning-list col-md-4">
{# <a href="{% url 'delete' pk=warns.pk %}"> #}
<button type="button" class="close">×</button>
{# </a> #}
<div class="publish-date"><strong>Warned:</strong> {{ warns.publish_date }}</div>
<div class="posted-by"><strong>Warned By: </strong> {{ warns.author }}</div>
<div class="warningtype"><strong>Warning Type: </strong>{{ warns.WarningType }}</div>
<div class="description">
<strong>Description:</strong> {{ warns.descript }}
</div>
{% endfor %}
$(".close").click(function () {
if (confirm("Are you sure you want to delete this?")){
$.ajax({
type: "DELETE",
url: "{% url 'delete' pk=warns.pk %}",
beforeSend: function(xhr) {
xhr.setRequestHeader("X-CSRFToken", getCookie("csrftoken"));
}
success: function() {
var close=$(this).parent();
close.fadeOut(1000);
});
}
})
}
});
$(".close").click(function () {
var close=$(this).parent();
if (confirm("Are you sure you want to delete this?")){
$.ajax({
type: "DELETE",
url: $(this).data('url'),
beforeSend: function(xhr) {
xhr.setRequestHeader("X-CSRFToken", getCookie("csrftoken"));
},
success: function() {
close.fadeOut(1000);
}
});
}
});