【问题标题】:Delete method with sweetalert in ruby on rails在 ruby​​ on rails 中使用 sweetalert 删除方法
【发布时间】:2017-09-19 06:34:23
【问题描述】:

您好,我是使用甜美警报 js 使我的警报框更加精美的新手。我正在使用普通的 javascript 警报确认来删除表中的特定数据。但是,当我尝试在删除它之前运行一个甜蜜的警报确认时,会删除文件而不会弹出确认。

下面是我的 JS 中的代码。

$(".delete-alert").click(function(event) {
  event.preventDefault();
  swal({
  title: 'Are you sure?',
  text: "You won't be able to revert this!",
  type: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes, delete it!'
}).then(function () {
  swal(
    'Deleted!',
    'Your file has been deleted.',
    'success'
  )
});
});

这是我的 ruby​​ on rails with HTML,当点击垃圾桶图标时调用上面的 js

<%= link_to raw('<i class="fa fa-trash delete-alert"></i>'), candidate_path(f.id),method: :delete %>

【问题讨论】:

标签: javascript jquery ruby-on-rails ruby sweetalert


【解决方案1】:

这是您应该如何使用 Sweet alert 的完美示例。

            swal({
                title: "Are you sure?",
                text: "You will not be able to recover this imaginary file!",
                type: "warning",
                showCancelButton: true,
                confirmButtonClass: 'btn-danger',
                confirmButtonText: 'Yes, delete it!',
                cancelButtonText: "No, cancel plx!",
                closeOnConfirm: false,
                closeOnCancel: false
            },
            function (isConfirm) {
                if (isConfirm) {
                    // Call your delete item function here.
                    swal("Deleted!", "Your imaginary file has been ted!", "success");
                } else {
                swal("Cancelled", "Your imaginary file is safe :)", "error");
                }
            });

【讨论】:

  • 我只是在我在 sweetalert 上确认后才询问如何删除该项目。截至目前,当我单击垃圾桶图标(在 ruby​​ 标记内使用)时,该项目正在被删除,而没有得到 sweetalert 的确认(sweetalert 框出现,但它不等待我的删除确认)。我的确切问题是在这个链接stackoverflow.com/questions/40579520/… 中提出并解决的,但它在 laravel 框架中。
  • 您还没有发布删除该项目的代码。但我可以建议你的是,你在if (isConfirm) {} 中调用删除项函数,所以只有当用户在甜蜜警报上单击确定并且如果用户单击取消时,才会调用删除项函数。
猜你喜欢
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 2012-11-06
  • 1970-01-01
  • 2015-06-13
  • 2021-07-18
  • 2014-02-23
相关资源
最近更新 更多