【问题标题】:using sweet alert instead of normal alert()使用甜蜜警报而不是普通警报()
【发布时间】:2018-09-29 04:24:51
【问题描述】:

当用户单击删除按钮时,我正在尝试发出甜蜜警报。

这是我的代码:

$(document).on('click', '.delete', function(){
    var course_id = $(this).attr("id");
    if(confirm("delete ?"))
    {
        $.ajax({
            url:"ajax/delete.php",
            method:"POST",
            data:{user_id,user_id},
            success:function(data)
            {
      swal("done", data, "success");
                dataTable.ajax.reload();
            }
        });
    }
    else
    {
        return false;
    }
});

而不是:

如果(确认)

我想使用甜蜜警报。

我尝试使用 swal 而不是确认,但它不起作用

我还是个初学者:)

谢谢

【问题讨论】:

标签: javascript jquery ajax


【解决方案1】:

试试这个

<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
//.deletebutton is the class name in button
<script>
    $('.deletebutton').on('click', function () {
        // return confirm('Are you sure want to delete?');
        event.preventDefault();//this will hold the url
        swal({
            title: "Are you sure?",
            text: "Once clicked, this will be softdeleted!",
            icon: "warning",
            buttons: true,
            dangerMode: true,
        })
        .then((willDelete) => {
            if (willDelete) {
                swal("Done! category has been softdeleted!", {
                    icon: "success",
                    button: false,
                });
            location.reload(true);//this will release the event
            } else {
                swal("Your imaginary file is safe!");
            }
        });
    });
</script>

【讨论】:

    【解决方案2】:

    您可以将确认按钮替换为类似以下内容:

    swal("A wild Pikachu appeared! What do you want to do?", {
      buttons: {
        cancel: "Run away!",
        catch: {
          text: "Throw Pokéball!",
          value: "catch",
        },
        defeat: true,
      },
    })
    .then((value) => {
      switch (value) {
    
        case "defeat":
          swal("Pikachu fainted! You gained 500 XP!");
          break;
    
        case "catch":
          swal("Gotcha!", "Pikachu was caught!", "success");
          break;
    
        default:
          swal("Got away safely!");
      }
    });
    

    https://sweetalert.js.org/guides/#advanced-examples

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 2018-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多