js和ajax调用删除记录:
$('.delete').click(function (e) {
if (confirm("Are you sure want to delete record?")) {
return true;
}
else {
e.preventDefault();
}
});
$('.type_delete').click(function() {
var csrf_token = $("#csrf_token").val();
var id = $(this).attr('id');
$.ajax({ // create an AJAX call...
data:{
csrfmiddlewaretoken: ('{{csrf_token}}'),
delete:id
},
type:'POST',
url: '/setting/type/', // the file to call
cache:false,
success: function() { // on success..
window.location.href = window.location;
}
});
return false;
});
});
视图.py
def types(request):
if request.method == 'POST':
if 'delete' in request.POST:
Types.objects.filter(id=request.POST['delete']).delete()
"""""""""
return render
html:
<input type="button" name="delete" value="Delete" id="{{ type.id }}" class="type_delete delete"/>
上面的 ajax 是从 html 中获取正确的 id 并删除该特定数据。删除工作正常,我正在使用删除确认对话框,问题是即使我按下取消按钮或关闭符号,数据也会被删除。它应该只有当我按下确定时才会发生。我需要帮助来解决这个问题。