【问题标题】:how to use alertify js with django to confirm either ok or cancel如何在 django 中使用 alertify js 来确认或取消
【发布时间】:2020-08-18 21:25:23
【问题描述】:

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

我试过了,但这是默认的警报浏览器

<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() 但似乎不起作用 感谢您的帮助..

【问题讨论】:

    标签: javascript django alertify alertifyjs


    【解决方案1】:

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

    为了达到预期的效果,您需要对 html 进行一些更改以传递当前元素(参考:this 关键字)和 就像examples section 中报告的那样,您可以将一个名为 showAlert 的函数附加到窗口对象:

    window.showAlert = function(msg, title, ele) {
        var that = ele;
        alertify.prompt(msg, title,
                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, title, ele) {
        var that = ele;
        alertify.prompt(msg, title,
                function(evt, value){
                    ele.closest('div').remove();
                },
                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>

    【讨论】:

    • 谢谢,但该脚本的问题是它在我单击按钮后删除,显示一个弹出窗口,然后在单击后立即删除对象
    • 它不会让我选择确定或取消,它永久选择确定
    • 确认任务的样子?
    • 我想询问用户何时用户想要删除帖子,(同意或不同意)如果选择同意(ok),则帖子将被删除,如果选择不同意(取消)不做任何事情帖子
    • 我很抱歉它不需要它,只是我需要这个alertifyjs.com/confirm.html 但我不知道为什么它在我的情况下不起作用
    猜你喜欢
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    • 2013-03-30
    • 2017-10-18
    • 1970-01-01
    • 2021-10-28
    • 2018-07-21
    相关资源
    最近更新 更多