如何与django一起使用alertify js确认确定或取消

问题描述

嗨,当有人要删除帖子时,我需要使用提醒消息,要求她/他删除或取消

我尝试过这个,但这是认的警报浏览器

<button onclick="return confirm('are you sure you want to delete {{obj.title}}')" class="bt mx-auto"><a href="{% url 'posts:post-detail' obj.id %}"><img src="{% static 'icons/delete.svg' %}" alt=""></a></button>

我要使用此警报

alertify.prompt("are you sure you want to delete ","{{obj.title}}",function(evt,value ){
alertify.success('Ok: ' + value);
  },function(){
alertify.error('Cancel');
  });

alertify

但是我不确定应该将该脚本放在模板的什么位置?我在按钮delete内使用它,替换为confirm(),但似乎不起作用 谢谢你的帮助..

解决方法

...我想问用户何时用户要删除帖子,(同意 或不同意)如果选择同意(ok),则该帖子将被删除,如果 选定的人不同意(取消)对帖子不做任何事情

为了获得所需的结果,您需要对html进行一些更改以传递当前元素(请参阅:此关键字),然后 像examples section中报告的那样,您可以将名为 showAlert 的函数附加到窗口对象:

window.showAlert = function(msg,title,ele) {
    var that = ele;
    alertify.prompt(msg,function(evt,value){
                ele.closest('div').remove();
            },function(){
                // do nothing 
            });
}

并将按钮更改为:

<button onclick="return showAlert('are you sure you want to delete','{{obj.title}}')".....

<script src="//cdn.jsdelivr.net/npm/alertifyjs@1.13.1/build/alertify.min.js"></script>
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/alertifyjs@1.13.1/build/css/alertify.rtl.min.css"/>
<script type="text/javascript">
window.showAlert = function(msg,function(){
                // do nothing
            });
}
</script>


<div>
    <p> This is the first post</p>
    <button onclick="return showAlert('are you sure you want to delete','post n. 1',this)">Click Me</button>
</div>

<div>
    <p> This is the second post</p>
    <button onclick="return showAlert('are you sure you want to delete','post n. 2',this)">Click Me</button>
</div>
<div>
    <p> This is the third post</p>
    <button onclick="return showAlert('are you sure you want to delete','post n. 3',this)">Click Me</button>
</div>